Laravel 5.1 带有问号的路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33685735/
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 5.1 Routes that have question mark
提问by Abdullah
I'm trying to create a route in Laravel 5.1 that will search the records base on "keyword". I like to include a ?
in my url for more readability. The problem is that when I'm including the ?
and test the route with postman it returns nothing. But when I remove the ?
and replaced it with /
and test it with postman again it will return the value of keyword. Does Laravel route supports ?
?
我正在尝试在 Laravel 5.1 中创建一条路线,该路线将根据“关键字”搜索记录。我喜欢?
在我的 url 中包含一个以提高可读性。问题是,当我包含?
并使用邮递员测试路线时,它什么也不返回。但是当我删除?
并替换它/
并再次用邮递员测试它时,它将返回关键字的值。Laravel 路由支持?
吗?
//Routes.php
Route::get('/search?keyword={keyword}', [
'as' => 'getAllSearchPublications',
'uses' => 'PublicationController@index'
]);
//Publication Controller
public function index($keyword)
{
return $keyword;
}
I've been searching the internet for hours now and I've read the Laravel documentation, But I can't find the answer. Thank you.
我已经在互联网上搜索了几个小时,并阅读了 Laravel 文档,但我找不到答案。谢谢你。
回答by Thomas Kim
I believe you are talking about query strings. To accept query parameters, you don't pass it as an argument. So, for example, your route should look more plain like this:
我相信你在谈论查询字符串。要接受查询参数,您不要将其作为参数传递。因此,例如,您的路线应该看起来更简单,如下所示:
Route::get('/search', [
'as' => 'getAllSearchPublications',
'uses' => 'PublicationController@index'
]);
Note: I dropped ?keyword={keyword}
.
注意:我丢弃了?keyword={keyword}
.
Then, in your controller method, you can grab the query parameter by calling the query
method on your Request
object.
然后,在您的控制器方法中,您可以通过调用对象query
上的方法来获取查询参数Request
。
public function index(Request $request)
{
return $request->query('keyword');
}
If you didn't already, you will need to import use Illuminate\Http\Request;
to use the Request
class.
如果您还没有,则需要导入use Illuminate\Http\Request;
才能使用Request
该类。
回答by Mrigendra Kashyap
Use
用
$resquest
$resquest
Parameter in your controller action to get the query parameter. Instead of using "?"
to create in your route.
控制器操作中的参数以获取查询参数。而不是"?"
用于在您的路线中创建。