Laravel使用url参数发布路径

时间:2015-08-07 07:54:42

标签: php laravel routing

我正面临一条大墙的幼虫路线,我似乎无法找到解决方案

我在视图模板中有这个表单

<form url="/request/{{$equipment->url}}" method="POST">
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="mt10">Start Date:</div>
                            <input type="date" required name="starting_date" value="" placeholder="From" class="request-input request-date mb10">
                        </div>
                        <div class="col-sm-4">
                            <div class="mt10">End Date:</div>
                            <input type="date" required name="ending_date" value="" placeholder="To" class="request-input request-date mb10">
                        </div>
                        <div class="col-sm-4">
                            <div class="mt10">Quantity</div>
                            <input type="number" required name="quantity" value="" placeholder="Quantity" class="request-input mb10">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="mt10">Voltage</div>
                            <input type="number" required name="voltage" value="" placeholder="Voltage" class="request-input mb10">
                        </div>
                        <div class="col-sm-4">
                            <div class="mt10">Param 1</div>
                            <input type="text" required name="param_1" value="" placeholder="Parameter" class="request-input mb10">
                        </div>
                        <div class="col-sm-4">
                            <div class="mt10">Param 2</div>
                            <input type="text" required name="param_2" value="" placeholder="Parameter" class="request-input mb10">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12">
                            <button class="btn btn-block button-orange">Get quotes now</button>
                        </div>
                    </div>
                </form> 

这是相应的路线

Route::group([ 'middleware' => 'rental'], function(){
 Route::get('/my-requests/{readby_url}', 'PagesController@requests');
 Route::post('/request/{equipment_url}', 'PagesController@request');
 Route::post('/request/create', 'RequestsController@create');
 Route::post('/request/accept', 'RequestsController@accept');
});

我的问题是Route::post('/request/{equipment_url}', 'PagesController@request'); ,因为当方法设置为发布时似乎不接受url参数。

即抛出错误

MethodNotAllowedHttpException in RouteCollection.php line 201:
in RouteCollection.php line 201
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 188
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 140
at RouteCollection->match(object(Request)) in Router.php line 746
at Router->findRoute(object(Request)) in Router.php line 655
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 237

我想传递一个参数并同时发布数据。

有没有办法让这项工作?我被告知Route :: post也可以处理GET,但它似乎无法正常工作。

3 个答案:

答案 0 :(得分:8)

问题与Laravel

无关
<form url="/request/{{$equipment->url}}" method="POST">

url替换为action

<form action="/request/{{$equipment->url}}" method="POST">

答案 1 :(得分:5)

HTTP POST动词不接受来自URL的参数,如GET,它从HTTP POST的Body接受它们。要获取帖子参数,请使用以下代码:

在routes.php中:

Route::post('/request', 'PagesController@request');

并在您的PagesController中使用以下

之一的输入方法访问表单输入
public function request() 
{
    return Input::get('equipment_url');
}

答案 2 :(得分:5)

您不能将获取参数发送到发布路径。

但是你可以通过简单的技巧实现这一目标,只需将你的价值({{$ equipment-&gt; url}})传递给表格的隐藏档案或会话。

例如:

HTML

<form url="test/{{$equipment->url}}" method="POST">
    {{Input::hidden('name-of-field', $equipment->url)
    <div class="row">
        .......
    </div>
</form>

路线

Route::post('test/{any-variable}', ['as' => 'test', 'uses' => 'TestController@test']);

控制器

public function test()
{
    echo "<pre>";
    dd(Input::all());
}

结果

array(1) {
           ["name-of-field"]=>
            string(5) "your value here"
          }