A controller that calls $this->middleware() or $this->authorize() crashes on Laravel 11 or 12
Your error
Call to undefined method App\Http\Controllers\Auth\LoginController::middleware()1 more line it can showFix it fast
Most common cause, 1 of 3The constructor calls $this->middleware()
Delete the constructor call and implement HasMiddleware with a static middleware() method.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
class LoginController extends Controller implements HasMiddleware
{
public static function middleware(): array
{
return [
new Middleware('guest', except: ['logout']),
new Middleware('auth', only: ['logout']),
];
}
}Short answer
Applications created with Laravel 11 or later get a base Controller that extends nothing, so the middleware() method and the AuthorizesRequests helpers from the old base class are gone. Code copied from older tutorials then fails. Implement the HasMiddleware interface with a static middleware() method, call Gate::authorize() instead of $this->authorize(), or add the traits back yourself.
Which cause do you have?
Look at the base controller your classes extend. If it is an empty abstract class, every helper the old one provided is missing.
cat app/Http/Controllers/Controller.php
grep -rnE "\\$this->(middleware|authorize|validate)\\(" app/Http/ControllersThen find the line you see in this list and open its fix:
- 1The constructor calls $this->middleware()
Call to undefined method ...::middleware(), thrown from a __construct that sets auth or guest middleware - 2Middleware belongs on the route rather than the class
Only one or two routes of the controller need the middleware - 3$this->authorize() comes from a trait the new base class lacks
Call to undefined method ...::authorize() inside a controller action
What does the error look like?
Call to undefined method App\Http\Controllers\Auth\LoginController::middleware()Call to undefined method App\Http\Controllers\PostController::authorize()
Cause 1 of 3
The constructor calls $this->middleware()
You seeCall to undefined method ...::middleware(), thrown from a __construct that sets auth or guest middleware
Check
The Laravel 12 skeleton's app/Http/Controllers/Controller.php is an empty abstract class. In Laravel 10 it extended Illuminate\Routing\Controller, which is where middleware() lived. Older auth scaffolding and tutorials still call it in the constructor.
grep -rn "this->middleware(" app/Http/ControllersFix
Delete the constructor call and implement HasMiddleware with a static middleware() method. The only and except options move into Middleware objects.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
class LoginController extends Controller implements HasMiddleware
{
public static function middleware(): array
{
return [
new Middleware('guest', except: ['logout']),
new Middleware('auth', only: ['logout']),
];
}
}Verify
List the routes with their middleware; the controller's routes show guest or auth as intended.
php artisan route:list -v --path=loginConfirmed by laravel.com
Cause 2 of 3
Middleware belongs on the route rather than the class
You seeOnly one or two routes of the controller need the middleware
Check
The controllers documentation shows middleware assigned in the route file as the first option. That keeps the controller free of framework wiring.
grep -n "LoginController" routes/web.phpFix
Attach the middleware to the routes or to a route group and remove it from the controller.
// routes/web.php
Route::middleware('guest')->group(function () {
Route::get('/login', [LoginController::class, 'show']);
Route::post('/login', [LoginController::class, 'store']);
});
Route::post('/logout', [LoginController::class, 'destroy'])->middleware('auth');Verify
Visiting /login while signed in redirects away, and logout still requires a session.
php artisan route:list -v --path=logConfirmed by laravel.com
Cause 3 of 3
$this->authorize() comes from a trait the new base class lacks
You seeCall to undefined method ...::authorize() inside a controller action
Check
The Laravel 10 base controller used the AuthorizesRequests and ValidatesRequests traits. The new skeleton does not, so $this->authorize() and $this->validate() disappear with them.
grep -rn "this->authorize(" app/Http/ControllersFix
Call the Gate facade, which throws the same AuthorizationException. If many controllers depend on the old helpers, add the traits to your base Controller once instead.
use Illuminate\Support\Facades\Gate;
public function update(Request $request, Post $post)
{
Gate::authorize('update', $post);
// ...
}
// or, app/Http/Controllers/Controller.php
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller
{
use AuthorizesRequests;
}Verify
A user without the permission gets a 403, an allowed user passes.
php artisan test --filter=PostPolicyConfirmed by laravel.com
How do I stop it coming back?
- When you copy controller code from an older guide, check which Laravel version it targets.
- Prefer route-level middleware or HasMiddleware over constructor calls in new code.
- Keep feature tests that hit guarded routes, so a missing middleware fails a test instead of production.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies: