Welcome To ITSolutionsGuides,
Lets see how to redirect from the controller and using the routes in laravel using simple functions like redirect() and redirect()->route() to more advanced techniques such as to_route(). After performing the functions in the controller we need to redirect the user to another page or we need to navigate the user to the another page. Whether you're aiming to redirect from a controller using the route name or dynamically redirecting to another route, we've got you covered. Therefore for redirecting using the routes laravel provides three different ways redirect(), redirect()->route(), to_route() . Lets see how to redirect from the controller using the routes in laravel using three different examples.
Lets create the routes for the redirect example.
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 the redirect() with 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 index(Request $request, $page = 1)
{
$users = User::get();
return redirect()->route("home");
}
}
Using the 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 index(Request $request, $page = 1)
{
$users = User::get();
return to_route("home");
}
}
Using the redirect() in the laravel.
<?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(Request $request, $page = 1)
{
$users = User::get();
return redirect("home");
}
}
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides