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

Rjs37's avatar

To clarify @crynobone 's last response, for me it was pushMiddleware not appendMiddleware. It must have been renamed at some point. So just summarising both sets of middleware:

public function boot(\Illuminate\Routing\Router $router, \Illuminate\Contracts\Http\Kernel $kernel) {
    //global middleware
    $kernel->prependMiddleware(\Path\To\Your\Middleware\custom_auth::class);
    $kernel->pushMiddleware(\Path\To\Your\Middleware\custom_auth::class);
    //router middleware
    $router->middleware('auth.user', \Path\To\Your\Middleware\custom_auth::class);
}
9 likes
updivision's avatar

Heads-up: in Laravel 5.4 it's aliasMiddleware for router middleware. So:

    $router->aliasMiddleware('auth.user', \Path\To\Your\Middleware\custom_auth::class);

Hope it helps someone.

7 likes
idff's avatar

Yes @updivision you saved my life too! Even though it changed in 5.4 it still worked with plain Middleware in my ServiceProvider, but now with 5.5 it does not and you 'have' to use aliasMiddleware. Just wasted a couple of days before I found this. Learnt a lot about Laravel in the process though so I suppose it's not all bad. Just got to catch up with all the work I should have been doing!

philthathril's avatar

Sorry if this is out of scope of the original question, but the above answers lead me to my solution of accessing previously-set properties in the terminate() method of my middleware, so I wanted to share what I did.

Using L5.5, I did not have to call $kernel->prependMiddleware() or $router->middleware() manually. I set my global middleware in the kernel.php along with all the other ones. Then in the AppServiceProvider I created a singleton of the middleware. The code starting with my middleware...

// Middleware.php
use My\Lovely;

class Middleware
{
    protected $keepMeAround;

    public function handle($request, \Closure $next, $guard = null)
    {
        $this->keepMeAround = "I'm here!";

        return $next($request);
    }

    public function terminate($request, $response)
    {
        echo $this->keepMeAround; // I'm here!
    }
}

And then the kernal...

// kernel.php
class Kernel extends HttpKernel
{
    protected $middleware = [
        My\Lovely\Middleware::class,
        ...
    ];
}

And finally the AppServiceProvider...

// AppServiceProvider.php
use My\Lovely\Middleware;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // @see https://laravel.com/docs/5.5/middleware#terminable-middleware
        //
        // Manually add this as a singleton so that when the terminate() method
        // is called on the middleware, we'll be using the same instance of
        // the originally-instantiated My\Lovely\Middleware.
        $this->app->singleton(Middleware::class, function ($app) {
            return new Middleware();
        });
    }
}

If the instructions found at https://laravel.com/docs/5.5/middleware#terminable-middleware are confusing to you, hopefully this will save you a few minutes.

Cheers.

mjlovefl's avatar

In case anyone else is looking for an answer. In your Service Provider, you simply need to get the app['router'] and add your Middleware to it. You can add it to a group, or as an alias to be used in the routes file (web.php etc.).

/**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot(): void
    {

        /** @var Router $router */
         $router = $this->app['router'];
         
        $router->aliasMiddleware('mymid', MyMiddleware::class);
        // or
        $router->pushMiddlewareToGroup('web', MyMiddleware::class);

    }
7 likes
Previous

Please or to participate in this conversation.