在Laravel v4中,我可以使用…

Route::currentRouteName()

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


当前回答

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

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

获取路由名称。

其他回答

解决方案:

$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;

你可以在模板中使用:

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

在控制器中访问当前路由名

Ie - http://localhost/your_project_name/edit

$request->segment(1);  // edit

(或)

$request->url();  // http://localhost/your_project_name/edit

在我看来,最简单的解决方案是使用这个助手:

request()->route()->getName()

有关文档,请参见这个链接

Laravel 5.2可以使用

$request->route()->getName()

它会给你当前的路由名。