Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

The constructor calls $this->middleware()

Delete the constructor call and implement HasMiddleware with a static middleware() method.

Fix
<?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']),
        ];
    }
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Laravel 11, Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
cat app/Http/Controllers/Controller.php
grep -rnE "\\$this->(middleware|authorize|validate)\\(" app/Http/Controllers

Then find the line you see in this list and open its fix:

  1. 1The constructor calls $this->middleware()Call to undefined method ...::middleware(), thrown from a __construct that sets auth or guest middleware
  2. 2Middleware belongs on the route rather than the classOnly one or two routes of the controller need the middleware
  3. 3$this->authorize() comes from a trait the new base class lacksCall 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.

Check
grep -rn "this->middleware(" app/Http/Controllers

Fix

Delete the constructor call and implement HasMiddleware with a static middleware() method. The only and except options move into Middleware objects.

Fix
<?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.

Verify
php artisan route:list -v --path=login

Confirmed by laravel.com

Did this fix it?

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.

Check
grep -n "LoginController" routes/web.php

Fix

Attach the middleware to the routes or to a route group and remove it from the controller.

Fix
// 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.

Verify
php artisan route:list -v --path=log

Confirmed by laravel.com

Did this fix it?

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.

Check
grep -rn "this->authorize(" app/Http/Controllers

Fix

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.

Fix
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.

Verify
php artisan test --filter=PostPolicy

Confirmed by laravel.com

Did this fix it?

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:

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.