【问题标题】:Laravel Request::is() - is there a better way?Laravel Request::is() - 有没有更好的方法?
【发布时间】:2017-04-10 19:03:54
【问题描述】:
@if(Request::is('login') OR Request::is('tags') OR Request::is('categories') OR Request::is('posts') OR Request::is('tags/..') OR Request::is('categories/..') OR Request::is('posts/..') OR Request::is("posts/{$post->id}"))
    @include('partials._navAdmin')
@else  
    @include('partials._nav')
@endif

以上是我的main.blade.php 文件中的一个示例;我正在尝试使用 2 个不同的导航栏 - 我知道有更好的方法可以做到这一点,但我仍然无法掌握它!

我认为一遍又一遍地重复Request::is 并不是好的编码标准。我是新手:(我错过了什么?

【问题讨论】:

    标签: php laravel laravel-blade laravel-request


    【解决方案1】:

    is() 方法迭代参数:

    foreach (func_get_args() as $pattern) {
        if (Str::is($pattern, $this->decodedPath())) {
            return true;
        }
    }
    

    所以,这样的事情应该适合你:

    @if(Request::is('login', 'tags', 'categories', 'posts', 'tags/..', 'categories/..', 'posts/..', 'posts/{$post->id}'))
    

    【讨论】:

    • 感谢您的快速回复。这确实很有帮助。
    【解决方案2】:

    对于 用户添加:- 请求::is('user/add');

    用户编辑:- 请求::is('user//edit'); 请求::is('user/edit/');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多