在Laravel v4中,我可以使用…
Route::currentRouteName()
我如何在Laravel v5和Laravel v6中做到这一点?
在Laravel v4中,我可以使用…
Route::currentRouteName()
我如何在Laravel v5和Laravel v6中做到这一点?
当前回答
你可以使用这行代码:url()->current()
在刀片文件中:{{url()->current()}}
其他回答
访问当前路由(v5.3起)
你可以在Route虚包上使用current, currentRouteName和currentRouteAction方法来访问处理传入请求的路由信息:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
参考Route虚包和Route实例的底层类的API文档,查看所有可访问的方法。
参考资料:https://laravel.com/docs/5.2/routing#accessing-the-current-route
你可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
您可以使用以下方法:
Route::getCurrentRoute()->getPath();
在Laravel版本> 6.0中,您可以使用以下方法:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
找到了一种查找当前路由名称的方法,适用于laravel v5, v5.1.28和v5.2.10
名称空间
use Illuminate\Support\Facades\Route;
and
$currentPath= Route::getFacadeRoot()->current()->uri();
对于Laravel Laravel v5.3,你可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
在Helper文件中,
您可以使用Route::current()->uri()来获取当前URL。
因此,如果你比较你的路由名在菜单上设置活动类,那么如果你使用
Route::currentRouteName()获取路由名并进行比较