Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

All about Redirects in Laravel 5

Tutorial last revisioned on August 11, 2022 with Laravel 9
Laravel has a useful function redirect() to, well, redirect a user to a different page or action, with or without data. Let's discuss those various options in one place - maybe you will find out something new for yourself. Notice: if you use Laravel 4, note that those functions changed and were simplified in Laravel 5 - for example, instead of Redirect::to() in new version we use just redirect(). For all the examples I will use a case of a Controller method, where the main action has been done, and now at the end it's time to redirect a user somewhere. So typically, the code snippets below represent the last row of Controller method.

Simple Redirect

This one is pretty straightforward - so if, for example, your website is www.website.com, it would redirect a user to the main URL + whatever you say in the parameter:
return redirect('homepage');
Redirects to http://www.website.com/homepage
return redirect('auth/login');
Redirects to www.website.com/auth/login
return redirect('');
Redirects to the main page - www.website.com

Chaining Methods and Redirecting Back

If you just want to redirect a user back to the previous page (the most common example - is to redirect back to the form page after data validation failed), you can use this:
return redirect()->back();
As you see in this example, you can add more methods to redirect() - even more than one, we'll see that in the next example.
Have you tried our tool to generate Laravel adminpanel without a line of code? Go to QuickAdminPanel.com

Redirect with Data

There are two methods which allow to send some data with the redirect. Firstly, you can just use with():
return redirect()->back()->with('error', 'Something went wrong.');
This code will add an item to the Session Flash Data, with key "error" and value "Something went wrong" - and then you can use that in the result Controller or View as session('error').
Reminder: Session Flash Data Laravel has a concept of Session Flash data for only one request - after the next is loaded, that Session element/value is deleted, and can't be retrieved again. This is exactly what is happening in with() method - its purpose is to add an error message or some input data only for that particular redirect and only for one page. It does the same thing as a function Session::flash('error', 'Something went wrong.').
You can also put several with() methods, one after another - it will save both entries into the Session:
return redirect()->back()->with('error', 'Something went wrong.')->with('order_value', $value);
But a more convenient way of doing that is to use arrays:
$parameters = ['error' => 'Something went wrong.', 'order_value' => $value];
return redirect()->back()->with($parameters);
If you're redirecting back to the form, a really useful method is withInput():
return redirect()->back()->withInput();
This method has no parameters and what it does is saves the old form values into Session. Then in the form you can use function old($key) to retrieve those values for every field - that would be a separate topic about the Forms.

Redirect to a Route

If in your routes.php file you have a route with a name, you can redirect a user to this particular route, whatever its URL is: routes/web.php:
Route::get('books', [BooksController::class, 'index'])->name('books_list');
app/Http/Controllers/SomeController.php
return redirect()->route('books');
This is really useful if in the future you want to change the URL structure - all you would need to change is routes.php (for example, get('books', ... to get('books_list', ...), and all the redirects would refer to that route and therefore would change automatically. And you can also use parameters for the routes, if you have any: routes/web.php:
Route::get('book/{id}', [BooksController::class, 'show'])->name('book_view');
app/Http/Controllers/SomeController.php
return redirect()->route('book_view', 1);
In case of more parameters - you can use an array: routes/web.php:
Route::get('book/{category}/{id}', [BooksController::class, 'show'])->name('book_view');
app/Http/Controllers/SomeController.php
return redirect()->route('book_view', [513, 1]);
Or you can specify names of the parameters:
return redirect()->route('book_view', ['category'=>513, 'id'=>1]);

Redirect to a Controller Action

The last piece of the puzzle is that you can redirect to a specific Method of a specific Controller, no matter what its Route or URL is - use method action():
return redirect()->action([BooksController::class, 'index']);
Note that if you worked with Laravel 4 previously, that in Laravel 5 you have to specify the whole path to the folder like that. Not very convenient, and in general I haven't seen much redirecting to specific actions in production code, the best and perhaps the most flexible way is to do it to named routes. Like in routes, you can redirect to the action with parameters - identical to the redirecting to routes with parameters, just put in the value or array of values as a second parameter of action() method:
return redirect()->action([BooksController::class, 'show'], ['id' => 1]);

Redirect to a External Domain

What if after some successfull action you want to redirect to external domain? As always laravel have you covered.
return redirect()->away('https://www.google.com');
Also Laravel now has helpers for redirect like action() or to_route(). You can check all helpers in the Official Documentation with examples. So, that's pretty much all the options of Laravel for redirecting the user to any page you want with any parameters you want. The most important trick here is not to redirect to the same page/action and not to cause eternal loop :)

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses