Inside this article we will see the concept i.e Laravel 9 Redirect to Route From Controller Example Tutorial. Article contains the classified information about route redirection in laravel 9 from controller.
If you are looking for a solution to redirection from controller in laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.
You mostly need to redirect the route from the controller method in the laravel project. Laravel provides several ways to return redirect with route name in laravel. Here, you will see you three ways to return a redirect to a specific route from the controller function.
Article will use redirect(), route() and to_route() functions for redirect to route. Only you need to pass the route name as an argument.
Learn More –
- JavaScript Remove All Event Listeners From an Element
- PHP Program to Count The Number of Vowels in a String
- Javascript Show Hide Div on Select Option Example Tutorial
- Laravel 9 How To Add Conditional Class in Blade Template
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Consider Application Test Routes
Open web.php from /routes folder. Let’s consider these test routes for this article.
//... use App\Http\Controllers\UserController; use App\Http\Controllers\HomeController; //... Route::get('users', [UserController::class, 'index']); Route::get('home', [HomeController::class, 'index'])->name("home"); //...
Example #1: Using redirect() with route()
Let’s consider a controller in application where we need to redirect from users to home route. We will use the concept of chained methods in 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()->route("home"); } }
Concept
return redirect()->route("home");
Example #2: Using to_route()
We will consider the same controller and same case as above.
<?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"); } }
Concept
return to_route("home");
Example #3: Using redirect()
We will consider the same controller and same case as above.
<?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"); } }
Concept
return redirect("home");
You can use any of the above methods for route redirection from laravel controllers.
We hope this article helped you to learn Laravel 9 Redirect to Route From Controller Example Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.