Thursday, June 16, 2016

How to redirect unlisted route to HomePage in Laravel?

redirected route



You can redirect all the unlisted or  unauthorized route to Homepage with a simple trick. But first we need to know why we need it. This is require when user try to access a route which is not in your application. So instead of showing 404 Error or NotFoundHTTPException you can easily redirect the user to Homepage.

It's very easy to implement. What you need to do is just run a query in  your routes.php file.
You can use the following code in your application in routes.php file. So when a user try to access the route which isn't in your routes list it will redirect  to the Homepage.

Route::any('{route}', function() { 
      return redirect('/');
 })->where('route', '.*');




Related Articles

0 comments:

Post a Comment