我正在使用Laravel 4。我想使用Laravel的刀锋模板引擎在视图中访问@if条件内的当前URL,但我不知道如何做到这一点。

我知道可以使用<?php echo URL::current();但是在@if blade语句中是不可能的。

有什么建议吗?


当前回答

class="nav-link {{ \Route::current()->getName() == 'panel' ? 'active' : ''}}"

其他回答

@if(request()->path()=='/path/another_path/*')
@endif

有两种方法:

<li{!!(Request::is('your_url')) ? ' class="active"' : '' !!}>

or

<li @if(Request::is('your_url'))class="active"@endif>

与使用URL::path()来检查当前路径位置不同,您可能需要考虑Route::currentRouteName(),以便万一您更新了路径,您不需要再次浏览所有页面来更新路径名称。

与 Laravel 5.4 相关:

全局函数

@if (request()->is('/'))
    <p>Is homepage</p>
@endif

对我来说,这是最好的方法:

class="{{url()->current() == route('dashboard') ? 'bg-gray-900 text-white' : 'text-gray-300'}}"