Laravel: How to Get Current Route Name? (v5 ... v7)

515,330

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>&nbsp; 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>&nbsp; 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();
Share:
515,330

Related videos on Youtube

Md Rashedul Hoque Bhuiyan
Author by

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, 2022

Comments

  • Md Rashedul Hoque Bhuiyan
    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
      Md Rashedul Hoque Bhuiyan almost 9 years
      which namespace should i use to find route name? i have used Illuminate\Support\Facades\Route but result is null.
    • lukasgeiter
      lukasgeiter almost 9 years
      That 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
      Kamil Kiełczewski about 8 years
    • Yevgeniy Afanasyev
      Yevgeniy Afanasyev almost 6 years
      Why would you need it?
  • bonbon.langes
    bonbon.langes about 8 years
    this also works when you put it on the view as {{ route(\Request::route()->getName()) }} . Thanks so much!
  • thelogix
    thelogix about 8 years
    This 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
    Fusion about 8 years
    Nah, I use this on daily basis. Check official docs
  • thelogix
    thelogix about 8 years
    I see. This only works in version 5.2 or greater. But its quite nice.
  • Rhys
    Rhys about 8 years
    This is actually incorrect. the name() method will add or change the name, while the getName() method returns it.
  • utdev
    utdev about 7 years
    Do you have an idea how to filter this for instance if one only wants to print in the view api routes api/...
  • EM-Creations
    EM-Creations over 6 years
    Route::currentRouteName(); perfect :)
  • Yevgeniy Afanasyev
    Yevgeniy Afanasyev almost 6 years
    Helper method is the best. Works with laravel-5.6
  • Daniel Dewhurst
    Daniel Dewhurst over 5 years
    $request::route()->getName() if you're already using the $request, or you can use the global helper request()::route()->getName().
  • 1stthomas
    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
    Samuel Shifterovich over 5 years
    Using the request() helper function is particularly useful in views. request()->route()->getName() is the best option.
  • Guja1501
    Guja1501 over 5 years
    request()->routeIs('route.name') latest way
  • Renato Liibke
    Renato Liibke almost 5 years
    also tested in Laravel 5.3
  • SimonDepelchin
    SimonDepelchin over 4 years
    There is also the Route::is('admin.*') that checks if the route name matches the given patterns.
  • Crasher
    Crasher about 4 years
    Thanks for a proper answer, lost 30 minutes trying useless suggestions.
  • Amir Asyraf
    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
    aspirinemaga about 4 years
    also tested in Laravel 7.5.2
  • Tariqul Islam
    Tariqul Islam almost 4 years
    tested in Laravel 5.7
  • EgoistDeveloper
    EgoistDeveloper over 3 years
    I think it is better choice in blade.
  • ilubis
    ilubis over 3 years
    agree with @DanielDewhurst you can check all the method in here Illuminate/Routing/Route
  • Kamlesh
    Kamlesh almost 3 years
    I 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
    Kamlesh almost 3 years
    any sugestion for laravel 8?
  • Hmerman6006
    Hmerman6006 over 2 years
    It should be request()->routeIs('home') in this instance to check route name or request()->is('home'). The latter Determines if the current request URI matches a pattern, while the former Determines if the route name matches a given pattern. So the former routeIs method I would suggest. Definitely not route()->is('home'). You will receive no such method error or route() method expecting 1 argument 0 given. [Source][ laravel.com/api/8.x/Illuminate/Http/Request.html ]
  • Kamlesh
    Kamlesh over 2 years
    Gettings error: Call to undefined method Laravel\Lumen\Routing\Router::current()