php Laravel:如何获取当前路由名称?(v5 ... v7)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30046691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: How to Get Current Route Name? (v5 ... v7)
提问by Md Rashedul Hoque Bhuiyan
In Laravel v4 I was able to get the current route name using...
在 Laravel v4 中,我能够使用...
Route::currentRouteName()
How can I do it in Laravel v5and Laravel v6?
我如何在Laravel v5和Laravel v6 中做到这一点?
回答by Adnan
Try this
尝试这个
Route::getCurrentRoute()->getPath();
or
或者
\Request::route()->getName()
from v5.1
从 v5.1
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel v5.2
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
检索请求 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:
path 方法返回请求的 URI。因此,如果传入请求的目标是http://example.com/foo/bar,则路径方法将返回foo/bar:
$uri = $request->path();
The ismethod 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:
该is方法允许您验证传入的请求 URI 是否与给定的模式匹配。*使用此方法时,您可以使用该字符作为通配符:
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,而不仅仅是路径信息,您可以在请求实例上使用 url 方法:
$url = $request->url();
Laravel v5.3 ... v5.8
Laravel v5.3 ... v5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Laravel 5.3 route documentation
Laravel v6.x...7.x
Laravel v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
** Current as of Nov 11th 2019 - version 6.5 **
** 截至 2019 年 11 月 11 日的当前版本 - 版本 6.5 **
Laravel 6.x route documentation
There is an option to use request to get route
有一个选项可以使用请求来获取路线
$request->route()->getName();
回答by loranger
Using Laravel 5.1, you can use
使用 Laravel 5.1,您可以使用
\Request::route()->getName()
回答by Md Rashedul Hoque Bhuiyan
Found a way to find the current route name works for laravel v5, v5.1.28and v5.2.10
找到了一种方法来查找适用于 laravel v5、v5.1.28和v5.2.10的当前路由名称
Namespace
命名空间
use Illuminate\Support\Facades\Route;
and
和
$currentPath= Route::getFacadeRoot()->current()->uri();
For Laravel laravel v5.3you can just use:
对于 Laravel laravel v5.3,您可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
回答by Fusion
If you need url, not route name, you do not need to use/require any other classes:
如果您需要url,而不是route name,则不需要使用/要求任何其他类:
url()->current();
回答by Webistan
If you want to select menu on multiple routes you may do like this:
如果您想在多条路线上选择菜单,您可以这样做:
<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i> Products</a></li>
Or if you want to select just single menu you may simply do like this:
或者,如果您只想选择单个菜单,您可以这样做:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
Also tested in Laravel 5.2
也在Laravel 5.2 中测试
Hope this help someone.
希望这有助于某人。
回答by Jalvin Vohra
Laravel 5.2 You can use
Laravel 5.2 你可以使用
$request->route()->getName()
It will give you current route name.
它会给你当前的路线名称。
回答by Jonathan
In 5.2, you can use the request directly with:
在 5.2 中,您可以直接使用请求:
$request->route()->getName();
or via the helper method:
或通过辅助方法:
request()->route()->getName();
Output example:
输出示例:
"home.index"
回答by WhipsterCZ
Shortest way is Route facade
\Route::current()->getName()
最短的方法是 Route 门面
\Route::current()->getName()
This also works in laravel 5.4.*
这也适用于 laravel 5.4.*
回答by JS Lee
You can use in template:
您可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
回答by Bogdan Ghervan
In a controller action, you could just do:
在控制器操作中,您可以执行以下操作:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$requesthere is resolved by Laravel's service container.
$request这里由 Laravel 的服务容器解决。
getName()returns the route name for named routesonly, nullotherwise (but you could still explore the \Illuminate\Routing\Routeobject for something else of interest).
getName()仅返回命名路线的路线名称,null否则(但您仍然可以探索该\Illuminate\Routing\Route对象以寻找其他感兴趣的东西)。
In other words, you should have your route defined like this to have "nameOfMyRoute" returned:
换句话说,您应该像这样定义您的路线以返回“nameOfMyRoute”:
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);

