Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 570
Md Rashedul Hoque Bhuiyan Avatar asked May 05 '15 07:05

Md Rashedul Hoque Bhuiyan


People also ask

What is the Laravel function to get the name of current route?

Accessing The Current Route The Route::current() method will return the route handling the current HTTP request, allowing you to inspect the full Illuminate\Routing\Route instance: $route = Route::current(); $name = $route->getName();

How do I find my route name in blade?

Example 1: Get current route name in Blade Files $route = Route::current(); dd($route); $name = $route->getName(); dd($name); $actionName = $route->getActionName(); dd($actionName); $name = Route::currentRouteName(); dd($name); $action = Route::currentRouteAction(); dd($action);


1 Answers

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(); 
like image 145
Adnan Avatar answered Sep 23 '22 06:09

Adnan