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');
}
You may use the current, currentRouteName, and currentRouteAction methods on the Route facade to access information about the route handling the incoming request:
Now in Laravel
5.3
I am seeing that can be made similarly you tried:https://laravel.com/docs/5.3/routing#accessing-the-current-route
Accessing The Current Route
Get current route name in Blade templates
{{ Route::currentRouteName() }} to get the name of route
for more info https://laravel.com/docs/5.5/routing#accessing-the-current-route
Looking at
\Illuminate\Routing\Router.php
you can use the methodcurrentRouteNamed()
by injecting a Router in your controller method. For example:or using the Route facade:
You could also use the method
is()
to check if the route is named any of the given parameters, but beware this method usespreg_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 aroundpreg_match()
which is a big subject in the PHP community.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:
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
I have used for getting route name in larvel 5.3
Request::path()
If you need url, not route name, you do not need to use/require any other classes: