Fixing the "unexpected 'Request' (T_STRING)" Parse Error in Laravel
You are encountering a ParseError because you have inadvertently created a class named Controller that shares the same name as a built-in Laravel namespace. In your code, you defined:
namespace App\Controllers;
use Illuminate\Http\Request;
class Controller
{
// ...
}
Laravel's service container relies heavily on autoloading to resolve dependencies and instantiate classes. When the framework attempts to load the App\Controllers\Controller class it just created, it conflicts with the standard Laravel convention where Illuminate\Routing\Controllers\Controller (or similar base controllers) might be referenced, but more critically, the error message itself points to a syntax issue within the class definition logic relative to how PHP parses property declarations.
However, looking closely at the specific error syntax error, unexpected 'Request' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST), this usually happens when a property type hint is placed incorrectly in the context of the file's compilation unit or if there is a naming conflict that confuses the parser during strict mode checks, though the primary culprit here is often the class name collision.
The most immediate fix is to rename your base controller class. Naming a custom base controller Controller is risky because it shadows the standard Laravel patterns and can cause autoloading ambiguities. Rename this file to BaseController.php or simply AppController.php. This ensures that when you extend it later, the hierarchy remains clear, but even without inheritance, unique naming prevents parser confusion in some IDEs and static analyzers.
Beyond renaming, there is a significant architectural issue with your __construct method regarding the use of Request::capture(). The Request facade does not have a capture() method in standard Laravel versions. This line:
$this->request = Request::capture();
will likely trigger a separate BadMethodCallException or an undefined method error if executed, but it doesn't explain the syntax error you are seeing immediately upon parsing. The syntax error is strictly about the class definition structure. If your IDE or PHP version is strict, defining a property $this->request = Request::capture(); inside the constructor while trying to use Request as a type hint elsewhere can be problematic if not handled correctly.
A better approach for handling requests in a base controller is to inject them via dependency injection rather than capturing them manually in the constructor. This aligns with Laravel's service container philosophy. Here is how you should refactor the class:
namespace App\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController; // Optional: if extending
class BaseController
{
public Request $request;
public function __construct(Request $request)
{
// Access the request instance directly passed by the container
$this->request = $request;
// Handle session logic only if necessary
if (key_exists('user', $_SESSION)) {
$this->user = $_SESSION['user'];
}
// Logic to redirect if user is not found and in panel route
if (count($this->user) === 0 && url()->contains('panel')) {
return redirect(url('/login-form'));
}
}
}
By accepting Request as a constructor argument, you allow Laravel's service container to provide the current request instance automatically. This eliminates the need for Request::capture() and ensures the property is typed correctly at runtime without parsing ambiguities. Additionally, moving the $this->user assignment outside the type-hinted property context or ensuring it is initialized properly avoids potential scope issues.
If you are extending this controller in your actual routes, make sure your child controllers extend App\Controllers\BaseController rather than inheriting from a generic Controller if one exists globally. This separation of concerns keeps your application modular and prevents the parser from getting confused about which Controller class is being referenced.
Remember that Laravel's internal classes often assume certain naming conventions. Breaking these by creating a file named exactly Controller.php in the App/Controllers directory can lead to autoloading collisions, especially if the framework expects an empty namespace root controller or if you are using traits that expect specific class signatures. Renaming the file resolves the immediate parse error and future-proofs your codebase against potential conflicts with framework updates.