Laravel post route with url parameters
我正面临着一大堵幼虫路线,我似乎找不到解决方案
我在视图模板中有这个表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <form url="/request/{{$equipment->url}}" method="POST"> Start Date: <input type="date" required name="starting_date" value="" placeholder="From" class="request-input request-date mb10"> End Date: <input type="date" required name="ending_date" value="" placeholder="To" class="request-input request-date mb10"> Quantity <input type="number" required name="quantity" value="" placeholder="Quantity" class="request-input mb10"> Voltage <input type="number" required name="voltage" value="" placeholder="Voltage" class="request-input mb10"> Param 1 <input type="text" required name="param_1" value="" placeholder="Parameter" class="request-input mb10"> Param 2 <input type="text" required name="param_2" value="" placeholder="Parameter" class="request-input mb10"> <button class="btn btn-block button-orange">Get quotes now</button> </form> |
这是对应的路线
1 2 3 4 5 6 | 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'); }); |
我的问题是
即它抛出错误
1 2 3 4 5 6 7 8 | 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,但它似乎不起作用。
问题与 Laravel 无关
1 | <form url="/request/{{$equipment->url}}" method="POST"> |
将
1 | <form action="/request/{{$equipment->url}}" method="POST"> |
您不能将获取参数发送到发布路由。
但是您可以通过一个简单的技巧来实现,只需在表单的隐藏字段或会话中传递您的值 ({{$equipment->url}})。
例如:
html
1 2 3 4 5 6 | <form url="test/{{$equipment->url}}" method="POST"> {{Input::hidden('name-of-field', $equipment->url) ....... </form> |
路线
1 | Route::post('test/{any-variable}', ['as' => 'test', 'uses' => 'TestController@test']); |
控制器
1 2 3 4 5 | public function test() { echo"[cc lang="php"]"; dd(Input::all()); } |
结果
1 2 3 4 |
HTTP POST 动词不接受来自 URL 的参数,如 GET,它接受来自 HTTP POST 正文的参数。要获取发布参数,请使用以下代码:
在 routes.php:
1 | Route::post('/request', 'PagesController@request'); |
并在您的 PagesController 中使用以下输入方法之一访问表单输入
1 2 3 4 | public function request() { return Input::get('equipment_url'); } |