Laravel - 检查刀片上的路线

Jos*_*ves 2 routes laravel laravel-blade

我需要检查 2 条路线,如果有一条路线,则添加一些内容

@if (Route::current()->uri() != '/' || Route::current()->uri() != 'login')<div>add some content some contnt </div> @endif
Run Code Online (Sandbox Code Playgroud)

我试过'||' 和“或”和“或”。我也尝试过Request::path(),它仅在检查 1 条路线时有效

@if (Route::current()->uri() != '/') <div>add some content some contnt </div> @endif
Run Code Online (Sandbox Code Playgroud)

如果我尝试 2 条路线,它似乎不起作用

Raj*_*ury 11

您可以使用内置方法来检查路由名称或模式。

Route::is()Route::currentRouteNamed()

例如:

Route::get('users', 'Controller..')->name('users.index');
Route::get('users/{id}', 'Controller..')->name('users.show');
Run Code Online (Sandbox Code Playgroud)

假设您在以下路径中: users/1

@if(Route::is('users.show') )
    // true
@endif

@if(Route::is('users') )
    // false
@endif

@if(Route::is('users.*') )
    // true
@endif
Run Code Online (Sandbox Code Playgroud)

所以在你的例子中尝试 @if(Route::is('home') or Route::is('login'))..@endif


sta*_*bit 7

你还可以这样做:

request()->routeIs('categories') 或者 request()->routeIs(['categories', 'services'])


Moh*_*taa 3

您需要使条件正确,因为您正在比较 url 和字符串。请尝试这个:

URL::current() != url('/')

Run Code Online (Sandbox Code Playgroud)