在Laravel v4中,我能够使用...来获取当前路由的名称。
Route::currentRouteName()
我该如何在Laravel v5和Laravel v6中做到这一点?
在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();
获取请求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 v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
**截至2019年11月11日当前版本为6.5**
可以使用请求来获取路由的选项。
$request->route()->getName();
request()->route()->getName()
。 - 1stthomasrequest()
辅助函数非常有用。request()->route()->getName()
是最佳选项。 - Samuel Shifterovich使用 Laravel 5.1,您可以使用
\Request::route()->getName()
{{ route(\Request::route()->getName()) }}
。非常感谢! - bonbon.langes找到了一种适用于 Laravel v5,v5.1.28 和 v5.2.10 的获取当前路由名称的方法。
命名空间
use Illuminate\Support\Facades\Route;
和
$currentPath= Route::getFacadeRoot()->current()->uri();
对于 Laravel v5.3,您只需使用以下代码:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
如果您想在多个路由中选择菜单,可以像这样操作:
<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>
或者,如果你只想选择单个菜单,可以像这样简单地执行:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
也在 Laravel 5.2 中进行了测试
希望这能帮助某些人。
如果你需要URL而不是路由名称,你不需要使用/要求任何其他类:
url()->current();
在 Laravel 7 或 8 中使用帮助函数。
Get Current Route Name
抱歉,我只能以英语进行交流和回答问题。request()->route()->getName()
为了检查路由是否当前,最好使用宏来创建自己的请求类方法。use Illuminate\Support\Facades\Request;
public function boot()
{
Request::macro('isCurrentRoute', function ($routeNames) {
$bool = false;
foreach (is_array($routeNames) ? $routeNames : explode(",",$routeNames) as $name) {
if(request()->routeIs($name)) {
$bool = true;
break;
}
}
return $bool;
});
}
你可以在Blade模板或控制器中使用此方法
request()->isCurrentRoute('foo') // string route
request()->isCurrentRoute(['bar','foo','xyz.*']) //array routes
request()->isCurrentRoute('blogs,foo,bar,xyz.*') //string route seperated by comma
您可以使用Laravel内置的路由方法
request()->routeIs('home');
request()->routeIs('blogs.*'); //using wildcard
request()->routeIs('home')
来检查路由名称或者request()->is('home')
。后者“确定当前请求的URI是否与模式匹配”,而前者“确定路由名称是否与给定模式匹配”。所以我建议使用前者routeIs
方法。绝对不是route()->is('home')
。您将收到“没有此类方法错误”或route()
方法预期1个参数,实际传递0个参数。[来源][ https://laravel.com/api/8.x/Illuminate/Http/Request.html ] - Hmerman6006Laravel 5.2您可以使用
$request->route()->getName()
它将为您提供当前路由名称。
name()
方法会添加或更改名称,而getName()
方法则返回它。 - Aron Rotteveel在5.2中,你可以直接使用请求:
$request->route()->getName();
或使用辅助方法:
request()->route()->getName();
输出示例:
"home.index"
访问当前路由
在Blade模板中获取当前路由名称
{{ Route::currentRouteName() }}
更多信息请参见https://laravel.com/docs/5.5/routing#accessing-the-current-route
最短路径是使用路由门面 \Route::current()->getName()
。
这在 Laravel 5.4.* 中同样适用。