Laravel Validator返回MethodNotAllowedHttpException错误

时间:2018-01-21 04:08:10

标签: php laravel validation

我有两个表单页面在我的路由中接受POST方法:

web.php

Route::post('select-room', ['as'=>'select-room','uses'=>'ClientBookingController@selectRoom']);
Route::post('book-room', ['as'=>'book-room','uses'=>'ClientBookingController@bookRoom']);

功能就像这样。我将页面'select-room'中的数据提交到'book-room'

方法'selectRoom'和'bookRoom'

中有验证
public function selectRoom(Request $request){
        $this->validate($request, [
            'start_date' => 'required|date|after:yesterday',
            'end_date' => 'required|date|after:start_date',
            'number_people' => 'required', 
            'number_kids' => 'required'
        ]);
 }
 public function bookRoom(Request $request){
        $validator = Validator::make($request->all(), [
            'start_date' => 'required|date|after:yesterday',
            'end_date' => 'required|date|after:start_date',
            'people' => 'required',
            'days_staying' => 'required',
            'free_days' => 'required',
            'room' => 'required|array',
            'roomCost' => 'required|array',
            'roomPeople' => 'required|array',
            'totalCost' => 'required',
            'name' => 'required|string|max:255',
            'email' => 'required|email|string|max:255|unique:users',
            'password' => 'required|string|min:6|max:255|confirmed'
        ]);

        if ($validator->fails())
        {
            return redirect()->back()->withErrors($validator)->withInput();
        }
 }

当我提交数据并且它没有在'bookRoom'方法中验证时,验证会向'select-room'网址发送一个get请求而不是post请求,最后会出现MethodNotAllowedHttpException错误。

我仍然理解Laravel你可能没有逻辑流程,你有一个帖子页面,然后发布到另一个帖子页面,如果在第二个帖子页面上失败将导致错误,因为你的路由只允许发布方法。验证功能是否限制您必须将get请求作为错误的偶然事件,或者我可以设置哪些可以在验证响应集中复制post请求?

2 个答案:

答案 0 :(得分:1)

要回答您的问题,您的选择房间路线方法应为any(),而不是post()

但是你的流程出了问题。您应该有一个显示选择的GET路线。 POST路由,您处理POST数据并在出现错误时返回GET路由。

答案 1 :(得分:0)

使用redirects执行任何GET。您的表单应为renderedGET方法,数据submittedPOST个请求。因此,使用get为URL创建一个新的book-room

Route::post('select-room', ['as'=>'select-room','uses'=>'ClientBookingController@selectRoom']);
Route::get('book-room', ['as'=>'book-room','uses'=>'ClientBookingController@showBookRoom']);
Route::post('book-room', ['as'=>'book-room','uses'=>'ClientBookingController@bookRoom']);

在你的控制器中 -

public function selectRoom(Request $request){
    $this->validate($request, [
        'start_date' => 'required|date|after:yesterday',
        'end_date' => 'required|date|after:start_date',
        'number_people' => 'required', 
        'number_kids' => 'required'
    ]);
    // Validate data
    // Persist data ready for displaying book-room using get

    return redirect('book-room');
}
public function showBookRoom()
{
    return //your new view with post data from previous url
}
public function bookRoom(Request $request){
    $validator = Validator::make($request->all(), [
        'start_date' => 'required|date|after:yesterday',
        'end_date' => 'required|date|after:start_date',
        'people' => 'required',
        'days_staying' => 'required',
        'free_days' => 'required',
        'room' => 'required|array',
        'roomCost' => 'required|array',
        'roomPeople' => 'required|array',
        'totalCost' => 'required',
        'name' => 'required|string|max:255',
        'email' => 'required|email|string|max:255|unique:users',
        'password' => 'required|string|min:6|max:255|confirmed'
    ]);

    if ($validator->fails())
    {
        return redirect()->back()->withErrors($validator)->withInput();
    }
 }

因此,只要您的第二篇文章fails,就会redirect使用URL请求GET。阅读 Laracast 中的this帖子,您将有更好的理解。