在Laravel v4中,我可以使用…

Route::currentRouteName()

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


当前回答

它不需要记忆任何东西,当你想请求某个变量时,通过dd(Request())可以评估哪个变量对你来说是突出的。 在下图中,它很清楚。

如果你想获取路径,显然,这段代码会显示

dd(request()->getpathInfo())

别忘了嵌入 使用说明\ Http \请求; 例如:

if(request()->getpathInfo()=="/logadmin"){
do somethings....
}

其他回答

Laravel 5.2可以使用

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

它会给你当前的路由名。

这是我使用的,我不知道为什么没有人提到它,因为它对我来说非常好。

Route::getCurrentRoute()->uri ; // this returns a string like '/home'

所以我在master.blade.php文件中使用它:

...

@if ( Route::getCurrentRoute()->uri =='/dashbord' )
   @include('navbar')
@endif
...

witch really helped me re use the same views without duplicating code.

使用Laravel 5.1,您可以使用

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

访问当前路由

在Blade模板中获取当前路由名

{{ Route::currentRouteName() }}

更多信息https://laravel.com/docs/5.5/routing#accessing-the-current-route

你可以使用这行代码:url()->current()

在刀片文件中:{{url()->current()}}