Laravel表单提交发送到错误的路线?

时间:2018-12-21 17:35:33

标签: laravel laravel-5.6

我有一个非常简单的表格:

{!! Form::open(['route' => ['complete.order']]) !!}
    {!! Form::hidden('date', \Carbon\Carbon::now()->format('F j, Y ')) !!}
    {!! Form::hidden('web_token', $order->web_token) !!}
    {!! Form::submit('Place this order', ['class'=>'btn btn-primary']) !!}
{!! Form::close() !!}

当我访问页面时,呈现如下:

<form method="POST" action="http://site.localhost/place-order" accept-charset="UTF-8">
    <input name="_token" type="hidden" value="kc6d4XoZ78RvJNtQbN8lavpLP7e1lI7rTGBvbeIP">
    <input name="date" type="hidden" value="December 21, 2018 ">
    <input name="id" type="hidden" value="15">
    <input class="btn btn-primary" type="submit" value="Place this order">
</form>

以下是相关路线:

Route::get('/orders/form', 'OrdersController@viewform')->name('orderform');
Route::post('/orders/review', 'OrdersController@review')->name('orders.review');
Route::post('/place-order', 'OrdersController@store')->name('complete.order');
Route::resource('/orders', 'OrdersController', ['except'=>['edit', 'update', 'destroy', 'show', 'store']])->middleware('auth');
Route::get('/orders/{order}', 'OrdersController@show')->name('orders.show');

当我单击“提交”按钮时,我被定向到site.localhost/orders/review,根据调试栏,它是作为GET请求而不是POST传递给的。

我不知道为什么会这样。表单应该转到site.localhost/place-order,当前仅输出return('place')进行测试。

OrdersController@show中的代码当前输出return('show')。我已经在所有OrdersController方法中进行了相同的测试。

添加php artisan route输出

+--------+-----------+-------------------------------+-----------------------+------------------------------------------------------------------------+------------------------------------------------------+
| Domain | Method    | URI                           | Name                  | Action                                                                 | Middleware                                           |
+--------+-----------+-------------------------------+-----------------------+------------------------------------------------------------------------+------------------------------------------------------+
|        | GET|HEAD  | /                             |                       | App\Http\Controllers\HomeController@index                              | web,auth                                             |
|        | GET|HEAD  | _debugbar/assets/javascript   | debugbar.assets.js    | Barryvdh\Debugbar\Controllers\AssetController@js                       | Barryvdh\Debugbar\Middleware\DebugbarEnabled,Closure |
|        | GET|HEAD  | _debugbar/assets/stylesheets  | debugbar.assets.css   | Barryvdh\Debugbar\Controllers\AssetController@css                      | Barryvdh\Debugbar\Middleware\DebugbarEnabled,Closure |
|        | DELETE    | _debugbar/cache/{key}/{tags?} | debugbar.cache.delete | Barryvdh\Debugbar\Controllers\CacheController@delete                   | Barryvdh\Debugbar\Middleware\DebugbarEnabled,Closure |
|        | GET|HEAD  | _debugbar/clockwork/{id}      | debugbar.clockwork    | Barryvdh\Debugbar\Controllers\OpenHandlerController@clockwork          | Barryvdh\Debugbar\Middleware\DebugbarEnabled,Closure |
|        | GET|HEAD  | _debugbar/open                | debugbar.openhandler  | Barryvdh\Debugbar\Controllers\OpenHandlerController@handle             | Barryvdh\Debugbar\Middleware\DebugbarEnabled,Closure |
|        | GET|HEAD  | api/user                      |                       | Closure                                                                | api,auth:api                                         |
|        | GET|HEAD  | confirm-account/{token}       |                       | App\Http\Controllers\UserController@create                             | web                                                  |
|        | GET|HEAD  | home                          | home                  | App\Http\Controllers\HomeController@index                              | web,auth                                             |
|        | GET|HEAD  | login                         | login                 | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest                                            |
|        | POST      | login                         |                       | App\Http\Controllers\Auth\LoginController@login                        | web,guest                                            |
|        | POST      | logout                        | logout                | App\Http\Controllers\Auth\LoginController@logout                       | web                                                  |
|        | GET|HEAD  | my-account                    | my-account.index      | App\Http\Controllers\UserController@index                              | web,auth                                             |
|        | POST      | my-account                    | my-account.store      | App\Http\Controllers\UserController@store                              | web,auth                                             |
|        | GET|HEAD  | my-account/create             | my-account.create     | App\Http\Controllers\UserController@create                             | web,auth                                             |
|        | PUT|PATCH | my-account/{my_account}       | my-account.update     | App\Http\Controllers\UserController@update                             | web,auth                                             |
|        | DELETE    | my-account/{my_account}       | my-account.destroy    | App\Http\Controllers\UserController@destroy                            | web,auth                                             |
|        | GET|HEAD  | my-account/{my_account}       | my-account.show       | App\Http\Controllers\UserController@show                               | web,auth                                             |
|        | GET|HEAD  | my-account/{my_account}/edit  | my-account.edit       | App\Http\Controllers\UserController@edit                               | web,auth                                             |
|        | POST      | orders                        | orders.store          | App\Http\Controllers\OrdersController@store                            | web,auth                                             |
|        | GET|HEAD  | orders                        | orders.index          | App\Http\Controllers\OrdersController@index                            | web,auth                                             |
|        | GET|HEAD  | orders/create                 | orders.create         | App\Http\Controllers\OrdersController@create                           | web,auth                                             |
|        | GET|HEAD  | orders/form                   | orderform             | App\Http\Controllers\OrdersController@viewform                         | web                                                  |
|        | POST      | orders/review                 | orders.review         | App\Http\Controllers\OrdersController@review                           | web                                                  |
|        | GET|HEAD  | orders/{order}                | orders.show           | App\Http\Controllers\OrdersController@show                             | web                                                  |
|        | POST      | password/email                | password.email        | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest                                            |
|        | POST      | password/reset                |                       | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest                                            |
|        | GET|HEAD  | password/reset                | password.request      | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest                                            |
|        | GET|HEAD  | password/reset/{token}        | password.reset        | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest                                            |
|        | POST      | place-order                   | complete.order        | App\Http\Controllers\OrdersController@store                            | web                                                  |
|        | POST      | register                      |                       | App\Http\Controllers\Auth\RegisterController@register                  | web,guest                                            |
|        | GET|HEAD  | register                      | register              | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest                                            |
|        | POST      | user/store                    | user.store            | App\Http\Controllers\UserController@store                              | web                                                  |
+--------+-----------+-------------------------------+-----------------------+------------------------------------------------------------------------+------------------------------------------------------+ 

2 个答案:

答案 0 :(得分:0)

好吧,我是个完全白痴。

问题是我正在使用自定义请求进行验证,但此路线不符合要求。因此,它实际上是在返回带有错误的页面,但由于我没有意识到这一点,所以没有显示任何错误。

答案 1 :(得分:0)

由于使用的是Route::resource,因此产生了您的问题,此路由定义了indexcreatestoreshoweditupdatedestroy的控制器路由,而您正试图覆盖store路由:

Route::post('/place-order', 'OrdersController@store')->name('complete.order');

您可以像这样使用更改表单代码:

{!! Form::open(['route' => ['orders.store']]) !!}

或者您可以在控制器中创建新功能:

public function newfucntion(Request $request)
{
    //All your code here
}

然后创建新路线:

Route::post('/place-order', 'OrdersController@newFunction')->name('complete.order');

然后像这样更改您的表单:

{!! Form::open(['route' => ['complete.order']]) !!}