如何使用 GET 方法将 GET 参数传递给 Laravel? [英] How To Pass GET Parameters To Laravel From With GET Method ?
问题描述
我被困在这个非常基本的表单上,我无法完成,我想构建一个带有文本输入和两个选择控件的搜索表单,以及一个接受 3 个参数的路由,问题是当我提交表单,它用问号映射参数,而不是 Laravel 方式,
i'm stuck at this very basic form, that i could not accomplish, which i want to build a search form with an text input, and two select controls, with a route that accept 3 parameters, the problem that when the i submit the form, it map the parameters with the question mark, not the Laravel way,
{{ Form::open(['route' => 'search', 'method' => 'GET'])}}
<input type="text" name="term"/>
<select name="category" id="">
<option value="auto">Auto</option>
<option value="moto">Moto</option>
</select>
{{ Form::submit('Send') }}
{{ Form::close() }}
路线
Route::get('/search/{category}/{term}', ['as' => 'search', 'uses' => 'SearchController@search']);
当我提交表单时,它会将我重定向到
When i submit the form it redirect me to
search/%7Bcategory%7D/%7Bterm%7D?term=asdasd&category=auto
如何使用 Laravel 方式将这些参数传递给我的路线,而无需使用 Javascript!:D
How can i pass these paramters to my route with the Laravel way, and without Javascript ! :D
推荐答案
最简单的方法就是接受传入的请求,在Controller中拉出你想要的变量:
The simplest way is just to accept the incoming request, and pull out the variables you want in the Controller:
Route::get('search', ['as' => 'search', 'uses' => 'SearchController@search']);
然后在SearchController@search
:
class SearchController extends BaseController {
public function search()
{
$category = Input::get('category', 'default category');
$term = Input::get('term', false);
// do things with them...
}
}
有用的是,您可以在Input::get()中设置默认值代码>
以防什么都没有传递给您的控制器操作.
Usefully, you can set defaults in Input::get()
in case nothing is passed to your Controller's action.
正如 joe_archer 所说,没有必要将这些术语放入 URL 中,作为 POST 可能更好(在这种情况下,您应该更新对 Form::open()
的调用,并且您在 routes.php 中的搜索路径 - Input::get()
保持不变)
As joe_archer says, it's not necessary to put these terms into the URL, and it might be better as a POST (in which case you should update your call to Form::open()
and also your search route in routes.php - Input::get()
remains the same)
这篇关于如何使用 GET 方法将 GET 参数传递给 Laravel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!