MethodNotAllowedHttpException thrown after user logs in and get redirected

不打扰是莪最后的温柔 提交于 2020-01-06 19:49:08

问题


Was testing around and this is the behaviour: I have a page with a form, anyone can use it (no need to be logged in), when submitting you get redirected to the next one for which you need to be logged in as a user. Here is the route:

        /*
    | Request Booking (POST)
    */
    Route::post('/booking/request-pay-booking', array(
        'as' => 'booking-request-pay-booking-post',
        'uses' => 'BookingController@postRequestPayBooking'
    ));

Everything works as long as you are already logged in BEFORE. If you are public when you click submit it redirects to the log in screen and once you click log in I tries to redirect to the page but I get a MethodNotAllowedHttpException thrown at me. I must be missing something there...

How would you handle that? Is it because it is a POST that takes me from one page to the other and the datas get lost?

Thanks!


回答1:


If you make any redirection you use for the path you want to redirect GET method, so you should probably change for some route from for example:

Route::post(

to

Route::match(['GET', 'POST']

So in case you make redirection to the route you put in the question you should change it into:

Route::match(['GET', 'POST'], '/booking/request-pay-booking', array(
    'as' => 'booking-request-pay-booking-post',
    'uses' => 'BookingController@postRequestPayBooking'
));



回答2:


Because you are not logged in before. Your request is not successfully done, and the login screen shows.

The request i.e. to your /booking/request-pay-booking url will be stored in the session, if you are using redirect intended within your login function then it is returning you to the page you requested before you logged in, after you have successfully logged in.

But not via POST via GET instead.

To overcome this allow both GET and POST to this route.

As you are only handling the request via post, allow for both methods. So post data should be stored somewhere else, for example; Session.

Store data within Session

Session::put('data', $data);

Get data from the Session

$data = Session::get('data');

If you do this then the method is not relying on a certain method to be used then use Marcin's solution;

Route::match(['GET', 'POST'], '/booking/request-pay-booking', array(
    'as' => 'booking-request-pay-booking-post',
    'uses' => 'BookingController@requestPayBooking'
));

Small change as the method handles both get and post change the name of the method to requestPayBooking.




回答3:


Thanks. Ended up doing something that works: created a new function that I redirect too when submitting the form, in this one I validate datas and store in a session the inputs. When the VALIDATE passes I redirect to the next view in which I extract the infos from the session and now even if I go through login screen it works. :)



来源:https://stackoverflow.com/questions/26142589/methodnotallowedhttpexception-thrown-after-user-logs-in-and-get-redirected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!