Laravel Request::is() - 有更好的方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40820907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Request::is() - is there a better way?
提问by vahan terzibashian
@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')
here is an example in my main.blade.php file, now what I'm doing is that I am trying to use 2 different navigation bars - I know there is a better way to do this but I still can't get the grasp of it!
这是我的 main.blade.php 文件中的一个示例,现在我正在尝试使用 2 个不同的导航栏 - 我知道有更好的方法来做到这一点,但我仍然无法掌握其中!
I don't think it is good coding standards to repeat Request::is over and over again. I am a newbie :( what did I miss over there?
我认为一遍又一遍地重复 Request::is 不是好的编码标准。我是一个新手 :( 我在那里错过了什么?
回答by Alexey Mezenin
is()
method iterates over arguments:
is()
方法迭代参数:
foreach (func_get_args() as $pattern) {
if (Str::is($pattern, $this->decodedPath())) {
return true;
}
}
So, something like this should work for you:
所以,这样的事情应该适合你:
@if(Request::is('login', 'tags', 'categories', 'posts', 'tags/..', 'categories/..', 'posts/..', 'posts/{$post->id}'))
回答by Chintan Hingrajia
For User Add :- Request::is('user/add');
对于用户添加 :- Request::is('user/add');
User Edit :- Request::is('user//edit'); Request::is('user/edit/');
用户编辑 :- Request::is('user //edit'); Request::is('用户/编辑/');