Laravel:如何获取当前路由名称?(v5...v7)

9 浏览
0 Comments

Laravel:如何获取当前路由名称?(v5...v7)

在 Laravel v4 中,我可以使用...获取当前路由名称。 在 Laravel v5 和 Laravel v6 中,我该如何实现?

Route::currentRouteName()

admin 更改状态以发布 2023年5月21日
0
0 Comments

使用Laravel 5.1,你可以使用

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

0
0 Comments

试一下

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

或者

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

从v5.1开始

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

Laravel v5.2

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

或者如果你需要操作名称

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

Laravel 5.2路由文档

获取请求URI

path方法返回请求的URI。因此,如果传入请求的目标是http://example.com/foo/bar,则path方法将返回foo/bar

$uri = $request->path();

is方法允许您验证传入请求URI是否与给定的模式匹配。在使用此方法时,可以使用*字符作为通配符:

if ($request->is('admin/*')) {
    //
}

要获取完整的URL而不仅仅是路径信息,可以在请求实例上使用url方法:

$url = $request->url();

Laravel v5.3 ... v5.8

$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();

Laravel 5.3路由文档

Laravel v6.x...7.x

$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();

** 截至2019年11月11日-版本6.5 **

Laravel 6.x路由文档

有一个使用请求来获取路由的选项

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

0