Laravel Validator返回MethodNotAllowedHttpException错误

我有两个表单页面接受我的路由中的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'url发送一个get请求而不是post请求,最后我得到一个MethodNotAllowedHttpException错误 .

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

回答(2)

2 years ago

任何 redirects 都是使用 GET 执行的 . 您的表单应为 rendered 作为 GET 方法,数据 submitted 作为 POST 请求 . 所以使用get为 book-room 创建一个新的 URL .

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 ,它将使用 GET 请求 redirect 到中间 URL . 从 Laracast 阅读this帖子,您将有更好的理解 .

2 years ago

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

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