简体   繁体   English

Laravel - Request::is() 无法正常工作

[英]Laravel - Request::is() doesn't work as it should

I have 4 different titles我有 4 个不同的标题

users用户

agencies机构

end customers最终客户

inactive不活跃

and i have only one view (one laravel.blade.php file) with this code我只有一个视图(一个 laravel.blade.php 文件)使用此代码

@section('title')
    @if (Request::is('/admin/users'))
        Alle Benutzer
    @elseif(Request::is('/admin/agencies'))
        Agenturen
    @elseif(Request::is('/admin/endcustomers'))
        Endkunden
    @else
        Inactive
    @endif
@endsection

and in my layout i have在我的布局中,我有

<title>@yield('title')</title>

and in my routes i have在我的路线中,我有

Route::get('/admin/users', 'AdminController@showUsers');
Route::get('/admin/agencies', 'AdminController@showAgencies');
Route::get('/admin/endcustomers', 'AdminController@showEndCustomers');
Route::get('/admin/inactive', 'AdminController@showInactiveUsers');

And which ever site i call the title is always Inactive What am i doing wrong....why the title doesn't change?我称之为标题的网站始终处于非活动状态 我做错了什么....为什么标题没有改变?

Well, you can't use the "Request" in the view.好吧,您不能在视图中使用“请求”。

I suggest you send the title from the controller like this:我建议你像这样从控制器发送标题:

return view('%the name of the view%', ['title'=>"%Your title%"]);

Then in the view, you can put this然后在视图中,你可以把这个

<title> {{$title }} </title>

you've got to echo the title in the @section.你必须在@section 中显标题。

@section('title')
    @if (Request::is('/admin/users'))
        echo("Alle Benutzer");
    @elseif(Request::is('/admin/agencies'))
        echo("Agenturen");
    @elseif(Request::is('/admin/endcustomers'))
        echo("Endkunden");
    @else
        echo("Inactive");
    @endif
@endsection

You can do like this in Blade (view) :你可以在Blade (view) 中这样做:

@switch(\Route::current()->getName())
    @case('/admin/users')
        Alle Benutzer
    @break

    @case('/admin/agencies')
        Agenturen
    @break

    @case('/admin/endcustomers')
        Endkunden
    @break

    @default
        Inactive
@endswitch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM