How to Get Current Route Name?

2019-01-05 08:47发布

In Laravel 4 I was able to get the current route name using...

Route::currentRouteName()

How can I do it in Laravel 5?

18条回答
Animai°情兽
2楼-- · 2019-01-05 09:28

Using Laravel 5.1, you can use

\Request::route()->getName()
查看更多
等我变得足够好
3楼-- · 2019-01-05 09:28

You can use in template:

<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
查看更多
Explosion°爆炸
4楼-- · 2019-01-05 09:28

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

查看更多
劫难
5楼-- · 2019-01-05 09:28

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

查看更多
老娘就宠你
6楼-- · 2019-01-05 09:32

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.

查看更多
Juvenile、少年°
7楼-- · 2019-01-05 09:34

Try this

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

or

\Request::route()->getName()

from v5.+

use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();

Laravel 5.3

Route::currentRouteName(); //use Illuminate\Support\Facades\Route;

Or if you need the action name

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

You can find everything about laravel Routes in the Laravel API: http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html

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();
查看更多
登录 后发表回答