php – redirect to a route using post method-ThrowExceptions

Exception or error:

i’ve registered a post route and set the “auth” middleware on it,everything is working properly but at the last step when i want to redirect to my registered route after authentication,an error occurred, it seems redirect() helper function uses GET method by default,but my route supports POST method.is there any way to use redirect() with POST method?!!

Route::post('match', 'HomeController@match')->name('match')->middleware('auth');

and inside my LoginController:

if ($is_match==='comes_from_match') {                   
    return redirect()->route('match');
}else{
    return redirect()->route('dashboard');
}

this leads to the following error:
“The GET method is not supported for this route. Supported methods: POST.”

How to solve:

Try the following:

You can use any instead of post it works for both get and post.

Route::any('match', 'HomeController@match')->name('match')->middleware('auth');

Answer´╝Ü

As per latest Laravel documentation:

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:

You can use match or any method:

Route::match(['get', 'post'], '/', function () {
    //
});

Route::any('/', function () {
    //
});

Reference:

Laravel -> Routing -> Basic Routing

Leave a Reply

Your email address will not be published. Required fields are marked *