php Laravel 发布带有 url 参数的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31872422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:41:10  来源:igfitidea点击:

Laravel post route with url parameters

phplaravelrouting

提问by Jad Salhani

I'm facing a big wall of larval routing and I can't seem to find a solution

我面临着幼虫路由的大墙,我似乎找不到解决方案

I have this form in a view template

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

<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> 

and this is the corresponding routes

这是相应的路线

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');
});

My issue is with Route::post('/request/{equipment_url}', 'PagesController@request'); as it seems to not accept url parameters when the method is set to post.

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

i.e it throws the error

即它抛出错误

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

I want to pass a parameter and post data at the same time.

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

Is there a way to make this work? I've been told that Route::post handles GET as well but it doesn't seem to work.

有没有办法使这项工作?有人告诉我 Route::post 也处理 GET,但它似乎不起作用。

回答by Jad Joubran

The problem has nothing to do with Laravel

问题与Laravel无关

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

replace urlwith action

替换urlaction

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

回答by Arlind

The HTTP POST verb doesn't accept parameters from the URL like GET, it accepts them from the Body of the HTTP POST. To fetch the post parameters you use the code below:

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

In routes.php:

在routes.php中:

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

and in your PagesController access the form input using one of the input methods such as below

并在您的 PagesController 中使用以下输入方法之一访问表单输入

public function request() 
{
    return Input::get('equipment_url');
}

回答by Anshul Mishra

You can not send get parameters to post route.

您不能发送获取参数来发布路由。

But you can achieve that by a simple trick, just pass your value ({{$equipment->url}}) in form's hidden filed or in session.

但是您可以通过一个简单的技巧来实现这一点,只需在表单的隐藏文件或会话中传递您的值 ({{$equipment->url}})。

For Example:

例如:

html

html

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

route

路线

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

controller

控制器

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

result

结果

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