Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation Issue: $this->middleware() causes "undefined method" error in Laravel #2271

Open
rodrigo1608 opened this issue Nov 25, 2024 · 2 comments

Comments

@rodrigo1608
Copy link

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:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }
}

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:

class AuthController extends \Illuminate\Routing\Controller
{
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }
}

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.

@CWard1975
Copy link

Another solution is to change the Controller.php file to:


use Illuminate\Routing\Controller as BaseController;

abstract class Controller extends BaseController
{
    //
}

This way all controllers will inherent Illuminate\Routing\Controller

@DrakkoFire
Copy link

With Laravel 11 things have changed a bit, now to set your middlewares inside the controllers you need to do as follows:

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
 
class UserController extends Controller implements HasMiddleware
{
    /**
     * Get the middleware that should be assigned to the controller.
     */
    public static function middleware(): array
    {
        return [
            'auth',
            new Middleware('log', only: ['index']),
            new Middleware('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:

Route::group(['prefix' => 'auth'], function () {
    Route::post('/register', [AuthController::class, 'register']);
    Route::post('/login', [AuthController::class, 'login']);
    Route::group(['middleware' => 'jwt.verify'], function () {
        Route::post('/refresh', [AuthController::class, 'refreshToken']);
        Route::post('/logout', [AuthController::class, 'logout']);
        Route::get('/user', [AuthController::class, 'getUser']);
    });
});

This way I have it organized in my API routes file, and it's easy to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants