微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何获取当前路由名称?

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

Route::currentRouteName()

我怎么能在Laravel 5中做到这一点?

解决方法:

试试这个

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

要么

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

来自v5.

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

Laravel 5.3

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

或者如果您需要动作名称

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

您可以在Laravel API中找到有关laravel Routes的所有信息:
http://laravel.com/api/5.0/Illuminate/Routing/Router.html
http://laravel.com/api/5.0/Illuminate/Routing.html

检索请求URI

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

$uri = $request->path();

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

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

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

$url = $request->url();

对于Laravel 5.2和5.3

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Laravel 5.3 route documentation

Laravel 5.2 route documentation

可以选择使用请求获取路由

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐