Md Rashedul Hoque Bhuiyan
Md Rashedul Hoque Bhuiyan

Reputation: 10641

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

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?

Upvotes: 325

Views: 700517

Answers (30)

rahmap
rahmap

Reputation: 57

You can use

url()->current();

it will result http://localhost:8000/your-path/...

Upvotes: -2

Jamal Azizbeigi
Jamal Azizbeigi

Reputation: 303

It doesn't need to memorize anything, when you would like some variable of Request, by dd(request()) can assess which variable is prominent for you. In the below image, it is clear. enter image description here

So, if you would like to get the path, obviously, this code will show

dd(request()->getpathInfo())

don't forget to embed use Illuminate\Http\Request; For instance:

if(request()->getpathInfo()=="/logadmin"){
do somethings....
}

Upvotes: 1

bravohex
bravohex

Reputation: 1054

use helper:

app('request')->route()->getName()

Upvotes: 1

Jignesh Joisar
Jignesh Joisar

Reputation: 15175

In laravel 7 or 8 use helper function

Get Current Route Name
request()->route()->getName()

To check if route is current better to create your own method for request class using macro

In AppServiceProvider In boot method :

use Illuminate\Support\Facades\Request;
public function boot()
    {
        Request::macro('isCurrentRoute', function ($routeNames) {
            $bool = false;
            foreach (is_array($routeNames) ? $routeNames : explode(",",$routeNames) as $name) {
               if(request()->routeIs($name)) {
                   $bool = true;
                   break;
                }
             }

             return $bool;
        });
    }

You can used this method in blade or controller

request()->isCurrentRoute('foo') // string route
request()->isCurrentRoute(['bar','foo','xyz.*']) //array routes
request()->isCurrentRoute('blogs,foo,bar,xyz.*') //string route seperated by comma

You can use inbuilt laravel route method

request()->routeIs('home');
request()->routeIs('blogs.*'); //using wildcard

Upvotes: 25

Yasser CHENIK
Yasser CHENIK

Reputation: 407

Here is what i use, i don't know why no one mentioned it, because it worked perfectly fine for me.

Route::getCurrentRoute()->uri ; // this returns a string like '/home'


So i use it in my master.blade.php file :

...

@if ( Route::getCurrentRoute()->uri =='/dashbord' )
   @include('navbar')
@endif
...

witch really helped me re use the same views without duplicating code.

Upvotes: 0

fatemeh sadeghi
fatemeh sadeghi

Reputation: 2603

use laravel helper and magic methods

request()->route()->getName()

Upvotes: 3

Darkcoder
Darkcoder

Reputation: 858

\Request::path()

I use this to get current uri

Upvotes: 0

Mhammed Talhaouy
Mhammed Talhaouy

Reputation: 1269

first thing you may do is import namespace on the top of class.

use Illuminate\Support\Facades\Route;

laravel v8

$route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // RouteName
$action = Route::currentRouteAction(); // Action

Laravel v7,6 and 5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Upvotes: 3

Hassan Tariq
Hassan Tariq

Reputation: 59

no one have answer if route name or url id needed on view direct for the route name on view direct

$routeName = Request::route()->getName();

for the id from url on view

$url_id = Request::segment(2);

Upvotes: 0

Jaied
Jaied

Reputation: 978

There are lots of ways to do that. You can type:

\Illuminate\Support\Facades\Request::route()->getName()

To get route name.

Upvotes: 0

Accesing the Current Route Name in Controller

ie - http://localhost/your_project_name/edit

$request->segment(1);  // edit

( or )

$request->url();  // http://localhost/your_project_name/edit

Upvotes: 1

Abd Abughazaleh
Abd Abughazaleh

Reputation: 5585

You can used this line of code : url()->current()

In blade file : {{url()->current()}}

Upvotes: 0

Adnan
Adnan

Reputation: 8200

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();

Upvotes: 612

yagnik devani
yagnik devani

Reputation: 103

You can use bellow code to get route name in blade file

request()->route()->uri

Upvotes: 5

worldask
worldask

Reputation: 1837

Request::path(); is better, and remember to use Request;

Upvotes: 4

vaibhavmht225
vaibhavmht225

Reputation: 25

You can use below method :

Route::getCurrentRoute()->getPath();

In Laravel version > 6.0, You can use below methods:

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Upvotes: 1

DenBrown
DenBrown

Reputation: 123

In my opinion the most easiest solution is using this helper:

request()->route()->getName()

For the docs, see this link

Upvotes: 5

panqingqiang
panqingqiang

Reputation: 167

$request->route()->getName();

Upvotes: 3

Kundan roy
Kundan roy

Reputation: 3132

Solution :

$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;

Upvotes: 1

yogesh chatrola
yogesh chatrola

Reputation: 431

Accessing The Current Route

Get current route name in Blade templates

{{ Route::currentRouteName() }}

for more info https://laravel.com/docs/5.5/routing#accessing-the-current-route

Upvotes: 9

MajidJafari
MajidJafari

Reputation: 1110

for some reasons, I couldn't use any of these solutions. so I just declared my route in web.php as $router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index']) and in my controller I got the name of the route using $routeName = $request->route()[1]['as']; which $request is \Illuminate\Http\Request $request typehinted parameter in index method of UserController

using Lumen 5.6. Hope it would help someone.

Upvotes: 0

Amitesh Bharti
Amitesh Bharti

Reputation: 15775

Accessing The Current Route(v5.3 onwards)

You may use the current, currentRouteName, and currentRouteAction methods on the Route facade to access information about the route handling the incoming request:

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Refer to the API documentation for both the underlying class of the Route facade and Route instance to review all accessible methods.

Reference : https://laravel.com/docs/5.2/routing#accessing-the-current-route

Upvotes: 4

Webistan
Webistan

Reputation: 357

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.

Upvotes: 28

WhipsterCZ
WhipsterCZ

Reputation: 657

Shortest way is Route facade \Route::current()->getName()

This also works in laravel 5.4.*

Upvotes: 6

Ken
Ken

Reputation: 3096

Looking at \Illuminate\Routing\Router.php you can use the method currentRouteNamed() by injecting a Router in your controller method. For example:

use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
   return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}

or using the Route facade:

public function index(Request $request) {
   return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}

You could also use the method is() to check if the route is named any of the given parameters, but beware this method uses preg_match() and I've experienced it to cause strange behaviour with dotted route names (like 'foo.bar.done'). There is also the matter of performance around preg_match() which is a big subject in the PHP community.

public function index(Request $request) {
    return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}

Upvotes: 2

Dipak Kolhe
Dipak Kolhe

Reputation: 23

I have used for getting route name in larvel 5.3

Request::path()

Upvotes: 1

ankit patel
ankit patel

Reputation: 1918

In a Helper file,

Your can use Route::current()->uri() to get current URL.

Hence, If you compare your route name to set active class on menu then it would be good if you use

Route::currentRouteName() to get the name of route and compare

Upvotes: 1

Jalvin Vohra
Jalvin Vohra

Reputation: 194

Laravel 5.2 You can use

$request->route()->getName()

It will give you current route name.

Upvotes: 15

Md Rashedul Hoque Bhuiyan
Md Rashedul Hoque Bhuiyan

Reputation: 10641

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();

Upvotes: 29

itsazzad
itsazzad

Reputation: 7277

Now in Laravel 5.3 I am seeing that can be made similarly you tried:

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

https://laravel.com/docs/5.3/routing#accessing-the-current-route

Upvotes: 4

Related Questions