如何使用GET方法将GET参数传递给Laravel? [英] How To Pass GET Parameters To Laravel From With GET Method ?

查看:920
本文介绍了如何使用GET方法将GET参数传递给Laravel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在这个非常基本的形式,我无法完成,我想要建立一个文本输入的搜索表单,和两个选择控件,接受3个参数的路线,当我提交表单,它将参数映射为问号,而不是Laravel的方式,

标记



<$ 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() ,以防万一传递给你的控制器的动作。

stackoverflow.com/a/27298740/658210\">作为joe_archer说,没有必要将这些条款放入URL中,作为POST可能更好(在这种情况下,您应该将呼叫更新为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆