在Laravel v4中,我可以使用…

Route::currentRouteName()

我如何在Laravel v5和Laravel v6中做到这一点?


当前回答

你可以在模板中使用:

<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>

其他回答

有很多方法可以做到这一点。你可以输入:

\Illuminate\Support\Facades\Request::route()->getName()

获取路由名称。

使用Laravel 5.1,您可以使用

\Request::route()->getName()

现在在Laravel 5.3中,我看到可以做出类似的尝试:

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

https://laravel.com/docs/5.3/routing#accessing-the-current-route

在larvel 5.3中,我用它来获取路由名

要求::路径()

你可以在模板中使用:

<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>