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

新手上路,请多包涵

在 Laravel v4 中,我能够使用…获取当前路线名称

Route::currentRouteName()

如何在 Laravel v5Laravel v6 中做到这一点?

原文由 Md Rashedul Hoque Bhuiyan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 628
2 个回答

尝试这个

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();

原文由 Adnan 发布,翻译遵循 CC BY-SA 4.0 许可协议

使用助手:

 app('request')->route()->getName()

原文由 bravohex 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题