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

martenkoetsier's avatar

Get all applied Middleware on current route

I'd like to present to the user some control depending on whether the current route has a specific middleware or not. In a blade file. Something along the line of:

@if (in_array('auth', Route::getCurrentRoute()->action['middleware']))
    show the control
@else
    show something else
@endif

When I dd'd the current route, it occurred to me that there are two places to look for the list of middleware applied. In the version 8.0 documentation on the Illuminate\Routing\Route class, there seem to be even more options. Which should I choose?

$mw = Route::getCurrentRoute()->action['middleware'];
$mw = Route::getCurrentRoute()->computedMiddleware;
$mw = Route::getCurrentRoute()->gatherMiddleware();
$mw = Route::getCurrentRoute()->middleware();
$mw = Route::getCurrentRoute()->controllerMiddleware();

Or even something else?

0 likes
7 replies
neilstee's avatar
neilstee
Best Answer
Level 34

@martenkoetsier I noticed that it didn't get the Controller's middleware so you have to use this one instead

Route::getCurrentRoute()->gatherMiddleware()
martenkoetsier's avatar

I'm sorry, but I don't see where I showed code about the authenticated user. Was this answer intended for some other question?

MoamenEltouny's avatar

It's ok never mind .. anyway if you just want to get the current route Middleware list you can use one of this

use Illuminate\Support\Facades\Route;

request()->route()->middleware();           // 1
Route::current()->computedMiddleware;		// 2
Route::getCurrentRoute()->middleware();		// 3
martenkoetsier's avatar

Thanks @neilstee and @pharaonic for your input! Good to see that I am not the only one in doubt as you both provide contradicting answers.

Looking at the framework code again, it looks like:

  • middleware(), withoutMiddleware(), and excludedMiddleware() public functions deal with the middleware from the action;
  • controllerMiddleware() public function deals with the middleware from the controller;
  • gatherMiddleware() public function deals with middleware from both the action and controller (if applicable) and stores this information in the (public) property computedMiddleware.

That would indicate that gatherMiddleware() is the most reliable source for getting all middleware applied to the route.

The question then rises why the computedMiddleware property is public, as its value might not have been determined yet. But that's a different question.

2 likes

Please or to participate in this conversation.