检查Laravel 5中的通配符路由

时间:2016-12-16 10:07:56

标签: laravel routing

在刀片中,如果我们想检查当前路线是否与路线匹配,我们可以简单地使用:

@if(Route::currentRouteName() == 'parameter')
{{ 'yes' }}
@else
{{ 'no' }}
@endif

但是,如果我们想要将它与通配符匹配,例如:

@if(Route::currentRouteName() == 'parameter.*')
{{ 'yes' }}
@else
{{ 'no' }}
@endif

有没有解决方案?

我尝试了“*”和“:any”,但它没有用。

注意:我想检查路线,而不是网址。

任何帮助都将不胜感激。

谢谢,

Parth Vora

3 个答案:

答案 0 :(得分:2)

使用Laravel的string helper功能

str_is('parameter*', Route::currentRouteName())

对于以parameter

开头的任何字符串,它都会返回true

答案 1 :(得分:1)

我有同样的问题。我想基于URI切换活动类。

在刀片服务器(Laravel 6x)中,我做到了:

try except ValueError

答案 2 :(得分:0)

您还可以使用Blades 自定义If语句,并在您的AppServiceProvider.php中编写如下内容:

public function boot()
{
    Blade::if('route', function ($route) {
        return Str::is($route, Route::currentRouteName());
    });
}

然后您可以在刀片视图中使用它,如下所示:

<li @route('admin.users*') class="active" @endroute>
    <a href="{{ route('admin.users.index') }}">Users</a>
</li>
相关问题