gloton's avatar

How to explicitly check all middlewares applied to a route in Laravel 12?

Hello everyone,

I'm working on a Laravel application (currently on a version identified as Laravel 12, which I understand follows the structure introduced in Laravel 11, especially regarding the configuration in bootstrap/app.php).

I want to be able to explicitly check the full list of middlewares applied to each of my routes, including both the framework's default middlewares for groups, API, and web (e.g., SubstituteBindings, ThrottleRequests, etc.) and any custom middleware I've added.

I've tried using the php artisan route:list -v command. The output I get for my API routes is similar to this:

GET|HEAD        api/v1/quizzes ................................................................................... api.v1.quizzes.index › Api\V1\QuizController@index
                ⇂ api
GET|HEAD        api/v1/quizzes/{quiz} .............................................................................. api.v1.quizzes.show › Api\V1\QuizController@show
                ⇂ api

I understand that the ⇂ api symbol indicates that the API middleware group is being applied to these routes. However, this output doesn't explicitly break down all the individual middlewares that make up that API group (including the framework's defaults and any I may have added).

In previous versions of Laravel, or with certain configurations, I recall that route:list -v (or using the --columns option when available) provided a more detailed listing of each applied middleware class.

My configuration in bootstrap/app.php to add a custom middleware to the API group is:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        apiPrefix: 'api',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // Alias para mi middleware
        $middleware->alias([
            'validate-jsonapi-headers' => \App\Http\Middleware\ValidateJsonApiHeadersMiddleware::class,
        ]);

        // Añadiendo mi middleware al grupo 'api'
        $middleware->api(append: [
            \App\Http\Middleware\ValidateJsonApiHeadersMiddleware::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

My questions are:

  1. Is there any other option or command in Laravel 11/12 that explicitly shows all middleware (classes or aliases) applied to a specific route, including those that are part of the default API or web groups?

  2. If the route:list -v output I show is what I expect for these versions, is there a programmatic way (e.g., by accessing the router or routes collection) to obtain this detailed information for debugging or auditing purposes?

  3. Has the way route:list -v presents this information changed in newer versions of Laravel?

I know I can rely on the framework applying the defaults, but for debugging and to have a full understanding of the execution stack, it would be very helpful to be able to see the explicit list.

Any help or clarification would be greatly appreciated!

Thanks in advance.

0 likes
3 replies
Glukinho's avatar
dd(Route::gatherRouteMiddleware(Route::getCurrentRoute()));

gives:

array:7 [▼ // routes\web.php:12
  0 => "Illuminate\Cookie\Middleware\EncryptCookies"
  1 => "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse"
  2 => "Illuminate\Session\Middleware\StartSession"
  3 => "Illuminate\View\Middleware\ShareErrorsFromSession"
  4 => "Illuminate\Foundation\Http\Middleware\ValidateCsrfToken"
  5 => "Illuminate\Routing\Middleware\SubstituteBindings"
  6 => "App\Http\Middleware\TestMiddleware"
]
3 likes
martinbean's avatar

@gloton You were almost there. Just increase the verbosity level to get each individual middleware class listed separately:

php artisan route:list -vv
1 like
Artwork's avatar

This is the answer. Though, it does not indicate middleware aliases but classes. Thank you very much, @martinbean !

1 like

Please or to participate in this conversation.