How To Redirect Routes & View Routes In Laravel

Hello there ! Today we are going to explore how to redirect our routes and view routes in laravel.

1.How To Redirect Routes.

If we define route that redirects to another URl then we  can use Route::redirect method of laravel.

It takes two path. First one is url from where you wants to redirect to , and second one is destination where user will redirected to. also we can specify our own status code as third argument.

Also it provides convenient shortcut so we do not need to define a whole route or controller for just redirecting URl.

Route::redirect('/here', '/there');

To define your own status code you can use status code as third parameter.

Route::redirect('/dashboard', '/charts', 301);

By default status code for Route::redirect is 302 we can modify it according to our needs.

Here 301 could also be returned by permanentRedirect method.

Also  destination and status keyword can not be used in redirect routes as they are reserved keywords in laravel.

2. How To View Routes

If  we only wants to return view for particular route then we can use the view  method of route.

As like redirect method it provide convenient way so we do not need to define a full route or controller.

It accepts two arguments. First one is the URl and  second one is view name  here view name and we can also pass the array of data as third argument

for particular view

Let’s take notification example for view routes.

we can define notification view  inside our routes file as:

Route::view('/notification', 'notification');

With data as third argument

Route::view('/notification', 'notification', ['name' => 'Taylor']);

Also , we need to keep in mind that  view, data, status, and headers are reserved keywords in laravel.

So they can not be used as route parameters in view routes.

That’s all for today. Thanks for read

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories