Search code examples
phplaravellaravel-routing

Redirect to a route if the request is not correct


My problem is to redirect a user if they try to access a page with the request method not defined in the 'web' route. Currently this throws a Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException error:

The GET method is not supported for this route. Supported methods: POST.


Solution

  • You can leverage the Fallback Route function

     Route::fallback(function ()
    {
        return redirect()->route('home'); // or redirect()->url('home')
    });
    

    Please note that the fallback route should always be the last route registered by your application https://laravel.com/docs/8.x/routing#fallback-routes