redirect()
is one of the most used and popular helper function. This function redirects users to different URLs of the website. By using this method, we can redirect the user to pages with or without data.
Redirect to different URL
This is the most and simplest form in which redirect method is used. We can redirect a user to homepage by simply using method below:
return redirect('/');
Users can also pass parameters inside redirect method. The parameters given in the method will be added to URL of base application.
return redirect('contact');
Likewise, users can also pass more parameters:
return redirect('user/profile');
back()
in Laravel
If the user wants to perform some sort of tasks and redirect back to the same page then the user can use back()
. This method is also really helpful.
return redirect()->back();
Redirect to Route
Users can also name the routes in the application for consistency. Some users call named routes instead of using URLs. Let’s see an example to make this concept clear.
return redirect()->route('profile.index');
We can also pass second parameter in route()
in the form of array. Lets see an example of parameterized route.
Route::get('post/{id}', 'PostController@edit')-name('post.edit');
We can write the above above in simpler form which is here below:
return redirect()->route('post.edit', ['id' => $post->id]);
If the parameter is single then the redirect method can be written as:
return redirect()->route('post.edit', $post->id);
Named routes are of great help because if the user wants to change the URL, the user doesn’t have to worry about updating redirects.
Redirecting with Data
Laravel has inbuilt methods for sending the data during redirection i.e. with()
and withInput()
.
At first, lets see an example of with method:
return redirect()->back()->with('success', 'Post saved successfully.');
Basically what the above code does is, it directs the user to the previous location with a value having a key success
in it. The data received can be retrieved in the controller or other pages.
session('success');
You can also pass multiple data in with()
like shown below:
$response = [
'success' => 'Profile updated successfully.',
'profile_id' => $profile->id,
];
return redirect()->back()->with($response);
Now, lets discuss about withInput()
. If the user is redirecting after some sort of form submission then the withInput()
method is used.
This method doesn’t take parameters instead of that it stores the values of form in session. Users can access those data by using old($key). The user has to provide the specified key to access the data.
Redirect to Controller
User can redirect to controller by simply using the action()
method.
return redirect()->action('PostController@edit);
Parameters can also be passed in action()
. Below is an example of that:
return response()->action('ProfileController@edit', ['id' => $profile->id]);
I absolutely love your site.. Great colors & theme.
Did you create this web site yourself? Please reply back
as I’m attempting to create my own personal blog and would like to find out where you got this from or what the
theme is called. Cheers!
This website is designed using generatepress marketer theme.
Great blog here! Also your site loads up fast! What host are you using?
Can I get your affiliate link to your host? I wish
my website loaded up as quickly as yours lol
Hostinger: https://www.hostg.xyz/SHDjt