You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documentation Issue: $this->middleware() causes "undefined method" error in Laravel
Hello,
I recently encountered an issue while following the JWT Auth documentation for Laravel. Specifically, in the section where the AuthController is implemented, the provided example uses $this->middleware in the constructor:
When running this code in my Laravel environment, I encountered the following error:
Call to undefined method App\Http\Controllers\AuthController::middleware()
After investigating, I discovered that the issue occurs because the Controller class being extended in the example refers to App\Http\Controllers\Controller, which doesn’t have the middleware() method. To fix this, I had to update my controller to extend the Illuminate\Routing\Controller class directly, like this:
It seems the documentation assumes the base Controller already provides the necessary functionality, which might not always be the case depending on the Laravel version or environment configuration.
Environment Details:
Laravel Version: 11
PHP Version: 8.3
Thank you for this fantastic package! I hope this feedback helps improve the onboarding experience for other users.
The text was updated successfully, but these errors were encountered:
With Laravel 11 things have changed a bit, now to set your middlewares inside the controllers you need to do as follows:
<?phpnamespaceApp\Http\Controllers;
useApp\Http\Controllers\Controller;
useIlluminate\Routing\Controllers\HasMiddleware;
useIlluminate\Routing\Controllers\Middleware;
class UserController extends Controller implements HasMiddleware
{
/** * Get the middleware that should be assigned to the controller. */publicstaticfunctionmiddleware(): array
{
return [
'auth',
newMiddleware('log', only: ['index']),
newMiddleware('subscribed', except: ['store']),
];
}
// ...
}
Here's the link for the Laravel documentation on how to use the middlewares inside the controllers. I personally prefer using groups in the routes directly, for example, my routes are as follow:
Documentation Issue:
$this->middleware()
causes "undefined method" error in LaravelHello,
I recently encountered an issue while following the JWT Auth documentation for Laravel. Specifically, in the section where the
AuthController
is implemented, the provided example uses$this->middleware
in the constructor:When running this code in my Laravel environment, I encountered the following error:
Call to undefined method App\Http\Controllers\AuthController::middleware()
After investigating, I discovered that the issue occurs because the Controller class being extended in the example refers to App\Http\Controllers\Controller, which doesn’t have the middleware() method. To fix this, I had to update my controller to extend the Illuminate\Routing\Controller class directly, like this:
It seems the documentation assumes the base Controller already provides the necessary functionality, which might not always be the case depending on the Laravel version or environment configuration.
Environment Details:
Thank you for this fantastic package! I hope this feedback helps improve the onboarding experience for other users.
The text was updated successfully, but these errors were encountered: