Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

Laravel 11 Middleware: Can I still do $this->middleware()->only([]) ?

I am all love for Laravel 11, except for the middleware part. Until now, I could define my Middleware, register it in the Kernel and then simply say:

$this->middlware('my-middleware')->only(['create', 'store', 'edit', 'update']) 

But that no longer seems to be the case. What's the best way to achieve the above functionality without having to assign the middleware to each individual route?

0 likes
4 replies
thebigk's avatar
Level 13

Larry AI does not seem to have read the Laravel 11 instructions. There's no Kernel.php in Laravel 11. :-/

puklipo's avatar

Controller ?

Not prohibited to back to Laravel10 style.

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;

abstract class Controller extends BaseController
{
    //
}

You can continue to use $this->middleware() in the controller.

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth')->only(['index']);
    }
}
s4muel's avatar
s4muel
Best Answer
Level 50

you mean to use that in controller? if so, check this: https://laravel.com/docs/11.x/controllers#controller-middleware

just implement HasMiddleware in the controller and add a static middleware method, where you can specify your only/except actions:

public static function middleware(): array
    {
        return [
            'auth',
            new Middleware('log', only: ['index']),
            new Middleware('subscribed', except: ['store']),
        ];
    }

(example from the documentation mentioned above)

1 like
thebigk's avatar
Level 13

Wow. This section is in 'Controllers'. Why is this not in Middleware where I expect it to find! Thank you for this.

1 like

Please or to participate in this conversation.