public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request here is resolved by Laravel's service container.
getName() returns the route name for named routes only, null otherwise (but you could still explore the \Illuminate\Routing\Route object for something else of interest).
In other words, you should have your route defined like this to have "nameOfMyRoute" returned:
In a controller action, you could just do:
$request
here is resolved by Laravel's service container.getName()
returns the route name for named routes only,null
otherwise (but you could still explore the\Illuminate\Routing\Route
object for something else of interest).In other words, you should have your route defined like this to have "nameOfMyRoute" returned:
In 5.2, you can use the request directly with:
or via the helper method:
Output example:
If you want to select menu on multiple routes you may do like this:
Or if you want to select just single menu you may simply do like this:
Also tested in Laravel 5.2
Hope this help someone.
Laravel 5.2 You can use
It will give you current route name.
Shortest way is Route facade
\Route::current()->getName()
This also works in laravel 5.4.*
Found a way to find the current route name works for laravel v5 , v5.1.28 and v5.2.10
Namespace
and
For Laravel laravel v5.3 you can just use: