Laravel: How to Get Current Route Name? (v5 ... v7)
Solution 1
Try this
Route::getCurrentRoute()->getPath();
or
\Request::route()->getName()
from v5.1
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel v5.2
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
Or if you need the action name
Route::getCurrentRoute()->getActionName();
Laravel 5.2 route documentation
Retrieving The Request URI
The path method returns the request's URI. So, if the incoming request is targeted at http://example.com/foo/bar
, the path method will return foo/bar
:
$uri = $request->path();
The is
method allows you to verify that the incoming request URI matches a given pattern. You may use the *
character as a wildcard when utilizing this method:
if ($request->is('admin/*')) {
//
}
To get the full URL, not just the path info, you may use the url method on the request instance:
$url = $request->url();
Laravel v5.3 ... v5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Laravel 5.3 route documentation
Laravel v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
** Current as of Nov 11th 2019 - version 6.5 **
Laravel 6.x route documentation
There is an option to use request to get route
$request->route()->getName();
Solution 2
Using Laravel 5.1, you can use
\Request::route()->getName()
Solution 3
Found a way to find the current route name works for laravel v5 , v5.1.28 and v5.2.10
Namespace
use Illuminate\Support\Facades\Route;
and
$currentPath= Route::getFacadeRoot()->current()->uri();
For Laravel laravel v5.3 you can just use:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
Solution 4
If you want to select menu on multiple routes you may do like this:
<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i> Products</a></li>
Or if you want to select just single menu you may simply do like this:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
Also tested in Laravel 5.2
Hope this help someone.
Solution 5
If you need url, not route name, you do not need to use/require any other classes:
url()->current();
Related videos on Youtube
Md Rashedul Hoque Bhuiyan
Present Senior Software Engineer - Team Lead at Orko Health Ltd. January 2021 to Continue Past Senior Software Developer at Orko Health Ltd. March 2019 to December 2020 Sr. Software Engineer at Health Care Information System Ltd. January 2017 to February 2019 Sr. Software Engineer at Incepsys from December 2015 to January 2017 Software Engineer at Liveoursource Ltd from September 2014 to October 2015 Believe in myself and love learning through helping others.
Updated on February 01, 2022Comments
-
Md Rashedul Hoque Bhuiyan about 2 years
In Laravel v4 I was able to get the current route name using...
Route::currentRouteName()
How can I do it in Laravel v5 and Laravel v6?
-
Md Rashedul Hoque Bhuiyan almost 9 yearswhich namespace should i use to find route name? i have used Illuminate\Support\Facades\Route but result is null.
-
lukasgeiter almost 9 yearsThat is the correct class. Your route has probably no name assigned. Note that the route name and the URI is not the same.
-
Kamil Kiełczewski about 8 yearsHere is right answer stackoverflow.com/questions/27397487/…
-
Yevgeniy Afanasyev almost 6 yearsWhy would you need it?
-
-
bonbon.langes about 8 yearsthis also works when you put it on the view as
{{ route(\Request::route()->getName()) }}
. Thanks so much! -
thelogix about 8 yearsThis returns an error: "Call to a member function current() on a non-object". url() returns a string, not an object, so i dont think this could ever have worked. Perhaps you were thinking about some other method or object, instead of url()?
-
Fusion about 8 yearsNah, I use this on daily basis. Check official docs
-
thelogix about 8 yearsI see. This only works in version 5.2 or greater. But its quite nice.
-
Rhys about 8 yearsThis is actually incorrect. the
name()
method will add or change the name, while thegetName()
method returns it. -
utdev about 7 yearsDo you have an idea how to filter this for instance if one only wants to print in the view api routes
api/...
-
EM-Creations over 6 years
Route::currentRouteName();
perfect :) -
Yevgeniy Afanasyev almost 6 yearsHelper method is the best. Works with laravel-5.6
-
Daniel Dewhurst over 5 years
$request::route()->getName()
if you're already using the $request, or you can use the global helperrequest()::route()->getName()
. -
1stthomas over 5 years@Daniel Dewhurst: Maybe it works on v < 5.7, but with v5.7 you must not use it statically, instead
request()->route()->getName()
is the way to go. -
Samuel Shifterovich over 5 yearsUsing the
request()
helper function is particularly useful in views.request()->route()->getName()
is the best option. -
Guja1501 over 5 years
request()->routeIs('route.name')
latest way -
Renato Liibke almost 5 yearsalso tested in Laravel 5.3
-
SimonDepelchin over 4 yearsThere is also the
Route::is('admin.*')
that checks if the route name matches the given patterns. -
Crasher about 4 yearsThanks for a proper answer, lost 30 minutes trying useless suggestions.
-
Amir Asyraf about 4 years@Jonathan I believe it's always better to use the full namespace in order to avoid any potential conflict.
-
aspirinemaga about 4 yearsalso tested in Laravel 7.5.2
-
Tariqul Islam almost 4 yearstested in Laravel 5.7
-
EgoistDeveloper over 3 yearsI think it is better choice in blade.
-
ilubis over 3 yearsagree with @DanielDewhurst you can check all the method in here
Illuminate/Routing/Route
-
Kamlesh almost 3 yearsI have used all 3 solutions in Laravel 8 version and none of them are working. // echo $route = Route::current(); echo $name = Route::currentRouteName(); echo $action = Route::currentRouteAction();
-
Kamlesh almost 3 yearsany sugestion for laravel 8?
-
Hmerman6006 over 2 yearsIt should be
request()->routeIs('home')
in this instance to check route name orrequest()->is('home')
. The latterDetermines if the current request URI matches a pattern
, while the formerDetermines if the route name matches a given pattern
. So the formerrouteIs
method I would suggest. Definitely notroute()->is('home')
. You will receive no such method error orroute()
method expecting 1 argument 0 given. [Source][ laravel.com/api/8.x/Illuminate/Http/Request.html ] -
Kamlesh over 2 yearsGettings error: Call to undefined method Laravel\Lumen\Routing\Router::current()