如何使用GET方法将GET参数传递给Laravel? [英] How To Pass GET Parameters To Laravel From With GET Method ?
问题描述
标记
<$ p $'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$' textname =term/>
< select name =categoryid =>
< option value =auto>自动< / option>
< option value =moto> Moto< / option>
< / select>
{{Form :: submit('Send')}}
{{Form :: close()}}
路线
路线:: get('/ search / {category} / {term }',['as'=>'search','uses'=>'SearchController @ search']);
当我提交表单时,它将我重定向到
搜索/%7Bcategory%7D /%7Bterm%7D?term = asdasd& category = auto
我怎样才能以Laravel的方式将这些参数传递给我的路线,而无需使用Javascript! :D
最简单的方法就是接受传入的请求,并在Controller中取出想要的变量:
路线:: get('search',['as'=>'search','uses'=>' SearchController @搜索']);
,然后在 SearchController @ search
:
类SearchController扩展BaseController {
公共功能搜索()
{
$ category = Input :: get('category','default category');
$ term = Input :: get('term',false);
//对他们做些事...
}
}
有用地,您可以在中设置默认值>输入:: get()
,以防万一传递给你的控制器的动作。
Form :: open()
以及您在route.php中的搜索路径 - Input :: get()
保持不变) 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,
Markup
{{ 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
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
How can i pass these paramters to my route with the Laravel way, and without Javascript ! :D
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']);
and then in SearchController@search
:
class SearchController extends BaseController {
public function search()
{
$category = Input::get('category', 'default category');
$term = Input::get('term', false);
// do things with them...
}
}
Usefully, you can set defaults in Input::get()
in case nothing is passed to your Controller's action.
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屋!