在Laravel v4中,我可以使用…
Route::currentRouteName()
我如何在Laravel v5和Laravel v6中做到这一点?
在Laravel v4中,我可以使用…
Route::currentRouteName()
我如何在Laravel v5和Laravel v6中做到这一点?
当前回答
最短的方法是Route facade \路线::当前()- > getName ()
这也适用于laravel 5.4.*
其他回答
现在在Laravel 5.3中,我看到可以做出类似的尝试:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
要求::路径();更好,记住使用Request;
找到了一种查找当前路由名称的方法,适用于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();
访问当前路由
在Blade模板中获取当前路由名
{{ Route::currentRouteName() }}
更多信息https://laravel.com/docs/5.5/routing#accessing-the-current-route
如果你想在多条路线上选择菜单,你可以这样做:
<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中也进行了测试
希望这能帮助到一些人。