Hello Geek, This article will teach us how to redirect from controller to route in Laravel. We’ll look at how to use Laravel redirects based on the route name. I’ll show you how to redirect a route in a Laravel controller using a simple example.
In the laravel project, you mostly need to redirect the route from the controller method. Laravel provides several methods for returning a redirect with a route name. Here are four methods for returning a redirect to a specific route from the controller function.
Route Examples:
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\HomeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('users', [UserController::class, 'index']);
Route::get('home', [HomeController::class, 'index'])->name("home");
Using redirect
() with route
()
App\Http\Controller\UserController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::get();
return redirect()->route("home");
}
}
Using to_route
()
App\Http\Controller\UserController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::get();
return to_route("home");
}
}
Using redirect
()
App\Http\Controller\UserController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::get();
return redirect("home");
}
}
Using route
() with Parameters
Here’s an example if you can pass the route parameters as the second argument to route()
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show(User $user)
{
return \Redirect::route('home', [$user->id])->with('message', 'User id found.');
}
}
If there is only one, there is no need to write it as an array:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show(User $user)
{
return \Redirect::route('home', $user->id)->with('message', 'User id found.');
}
}
If your route has more parameters, or if it only has one, but you want to clearly specify which parameter has which value (for readability), you can always do the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show(User $user)
{
return \Redirect::route('home', ['id'=>$user->id,'OTHER_PARAM'=>'XXX',...])->with('message', 'User id found.');
}
}
All the best nerd!