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?
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?
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();
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.
In a controller action, you could just do:
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:
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
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
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
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
In my opinion the most easiest solution is using this helper:
request()->route()->getName()
For the docs, see this link
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');
}
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.
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();