Laravel Redirect to Url


Laravel Redirect to Url : Laravel Redirects are basically instance of the Illuminate\Http\RedirectResponse. This contains the headers which are responsible to redirect the user from on url to another url. There are many ways to redirect the user from one url to another url. The simple method is use helper method of global helper helper. In this tutorial we are going to explain how to use redirect method in laravel to redirect the users from one url to another url.


Laravel Redirect to Url

Here are the redirection methods which are used in Laravel-

Simple Redirect-

Laravel Redirect to Url:

Route::get('users', function () {
    return redirect('home/users');
});

Which Will redirect user to home/users location.

More About Laravel Redirect to Url

Let us have more example on laravel redirect-

Laravel Redirect Back-

Laravel Redirect Back to url-

Route::get('users/login', function () {
   return back()->withInput();
});

Which Will redirect user to back to the previous location.

Laravel Redirect to Named Routes-

Laravel Redirect to Named Routes-

return redirect()->route('users');

Which Will redirect user to the route users.

Laravel Redirect to Named Routes With Parameters-

Laravel Redirect to Named Routes With Parameters-

return redirect()->route('users',['id' => 10]);

Which Will redirect user to the route users with parameter id and it’s value 10.

Laravel Redirect to Controller Action-

You can redirect to controller Action in laravel as below-

Laravel Redirect to Controller Action-

return redirect()->action('UsersController@index');

Which Will redirect user to the route index Action of UsersController Controller.

Laravel Redirect to Controller Action With Parameters-

You can redirect to controller Action with parameters in laravel as below-

Laravel Redirect to Controller Action With Parameters-

return redirect()->action('UsersController@index',['id' => 10]);

Which Will redirect user to the route index Action with parameter id and it’s value 10 of UsersController Controller.

Laravel Redirect with Flashed Session Data-

You can redirect flashed session data in laravel as below-

Laravel Redirect with Flashed Session Data-

Route::post('user/profile', function () {
    // Update the user's profile information...
    return redirect('home')->with('messgae', 'User's Profile updated Successfully!');
});

Which Will redirect user to the home with flashed session message ‘User’s Profile updated Successfully!’.

Note :Flashed Messages are shown only once and cleared after that.

Advertisements

Add Comment

📖 Read More