在Laravel中,构建路线采用以下形式:
Route::get('testing', function(){
return 0 //whatever
});
如果我想要包含查询字符串,以便我可以导航到localhost/testing?p1=blah&p2=blah2...
,是否有内置方法可以执行此操作?我唯一能想到的是使用单个变量并自己解析字符串:
Route::get('testing\\?{query}, function($query){ //I'm not sure if you have to escape the questionmark
//parse the string manually
return 0 //whatever
});
我觉得必须有一种内置的方式来做得更好一些,但我无法弄清楚它是什么。
答案 0 :(得分:3)
查询字符串不是路由的一部分。您可以使用request($key)
:
Route::get('testing', function(){
return request('p1');
});
您还可以通过调用all
方法获取查询字符串中所有内容的关联数组:
Route::get('testing', function(){
return request()->all();
});
注意:如果您使用的是旧版本的Laravel,请使用
Input::get()
&分别为Input::all()
。
答案 1 :(得分:0)
您也可以request()->all()
或request()->get('p1')