Laravel Redirect to URL using redirect() Helper
In this tutorial, i am going to tell you how to redirect user one page to another page from controller method. we normally use redirect() helper method for redirect user in controller.
We can simply use redirect() helper in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11. Laravel version provided redirect(). there are several way to do redirect URL in Laravel. In this post i am going to give you all the way to redirect URL with parameters.
There are several methods through we can redirect URL in Laravel 5 as listed bellow:
1) Laravel Redirect to URL
2) Laravel Redirect back to previous page
3) Laravel Redirect to Named Routes
4) Laravel Redirect to Named Routes with parameters
5) Laravel Redirect to Controller Action
6) Laravel Redirect to Controller Action With Parameters
7) Laravel Redirect with Flashed Session Data
Laravel Controller Redirect to URL
you can simply redirect given URL, bellow example i simple redirect "itsolutionstuff/tags" URL.
Route:
Route::get('users', 'UserController@tags');
Controller Method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return redirect('/users');
}
}
Laravel Controller Redirect Back to Previous Page
In this example, we can redirect back to our previous page URL, so you can do it both way:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
/* return redirect()->back(); */
return back();
}
}
Laravel Controller Redirect to Named Routes
If you declare route with name and you want to redirect route from controller method then you can simply do it.
Route:
Route::get('users', [UserController::class, 'show'])->name('users.index');
Controller Method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return redirect()->route('users.index');
}
}
Laravel Controller Redirect to Named Routes with Parameters
If you declare route with name and also parameters and you want to redirect route from controller method then you can do it by following example.
Route:
Route::get('/users/{id}', [UserController::class, 'show'])->name('users.show');
Controller Method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return redirect()->route('users.show', 2);
}
}
Laravel Controller Redirect to Controller Action
we can also redirect controller method using action of redirect() helper as you can see bellow example:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return redirect()->action('App\Http\Controllers\HomeController@home');
}
}
Laravel Controller Redirect to Controller Action With Parameters
we can also redirect controller method using action with parameters of redirect() helper as you can see bellow example:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return redirect()->action('App\Http\Controllers\HomeController@home',['id'=>17]);
}
}
Laravel Controller Redirect with Flashed Session Data
we can also pass flashed session message while redirect with routes or url in controller method as you can see bellow example.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
return redirect('home')->with('message', 'Welcome to ItSolutionStuff Tutorials!');
}
}
You can simply redirect above ways to URL from controller method.
Maybe it can help you....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Delete File After Download Response Example
- How to use Carbon in Laravel Blade or Controller File?
- How to Call Controller Function in Blade Laravel?
- Laravel Redirect to Route from Controller Example
- Laravel Call Function from Same Controller Example
- How to Force Redirect HTTP to HTTPS in Laravel?
- Laravel Redirect Back with Input and Error Messages Example
- How to Call Middleware from Controller in Laravel?
- Laravel Response Download File Example
- Laravel Multiple Files Download with Response Example
- How to Return JSON Response in Laravel?
- How to Redirect to External URL in Laravel?
- How to Redirect Route with Querystring in Laravel?
- Laravel Redirect Back to Previous Page After Login Example