Laravel:如何获取当前路线名称? (v5 ... v7) [英] Laravel: How to Get Current Route Name? (v5 ... v7)

查看:119
本文介绍了Laravel:如何获取当前路线名称? (v5 ... v7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel v4中,我可以使用...获取当前的路线名称.

 Route::currentRouteName()
 

如何在 Laravel v5 Laravel v6 中进行操作?

解决方案

尝试一下

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

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?

解决方案

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

这篇关于Laravel:如何获取当前路线名称? (v5 ... v7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆