Laravel - Redirections
Laravel - Redirections
Section titled “Laravel - Redirections”Redirecting to Named Routes
Section titled “Redirecting to Named Routes”Named routes allow you to refer to routes by a specific name, making your application more maintainable. You can assign a name to a route using the name() method chained onto the route definition.
Route::get('user/profile', function () { // Logic for user profile})->name('profile');Note: Here, we have given the name “profile” to the route user/profile.
To redirect to a named route, you can use the route() helper method on the redirect() instance:
return redirect()->route('profile');Example
Section titled “Example”Step 1: Create a view called profile_display.blade.php and save it at resources/views/profile_display.blade.php.
resources/views/profile_display.blade.php
<html> <body> <h1>User Profile Page (Reached via Named Route)</h1> </body></html>Step 2: In routes/web.php, set up the route for the profile_display.blade.php view and name it user.profile.page. Also, set up another route /go-to-profile which will redirect the request to the named route user.profile.page.
routes/web.php
Route::get('/user/actual-profile', function(){ return view('profile_display');})->name('user.profile.page');
Route::get('/go-to-profile', function(){ return redirect()->route('user.profile.page');});Step 3: Visit the following URL to test the named route redirection.
http://localhost:8000/go-to-profile
Step 4: After execution of the above URL, you will be redirected to http://localhost:8000/user/actual-profile because the /go-to-profile route redirects to the named route user.profile.page.
Step 5: The browser will display the content of profile_display.blade.php, which is ‘User Profile Page (Reached via Named Route)’.
Redirecting to Controller Actions
Section titled “Redirecting to Controller Actions”You can also redirect to controller actions. Pass the controller class and the action method name (as a string or an array) to the action() method. If your controller route requires parameters, you can pass them as a second argument to the action() method.
// Without parametersreturn redirect()->action([UserProfileController::class, 'show']);
// With parametersreturn redirect()->action([PhotoController::class, 'show'], ['id' => 1]);Example
Section titled “Example”Step 1: Execute the below command to create a controller called DashboardController.
php artisan make:controller DashboardControllerStep 2: After successful execution, you will see a message like ‘Controller created successfully.’ in your console.
Step 3: Copy the following code into the file app/Http/Controllers/DashboardController.php.
app/Http/Controllers/DashboardController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller{ public function index(){ return "Welcome to your Dashboard!"; }}Step 4: Add the following lines in routes/web.php.
routes/web.php
use App\Http\Controllers\DashboardController;
// The actual route handled by the controllerRoute::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.show');
// A route that redirects to the controller actionRoute::get('/redirect-to-dashboard', function(){ return redirect()->action([DashboardController::class, 'index']);});Step 5: Visit the following URL to test the redirection to a controller action.
http://localhost:8000/redirect-to-dashboard
Step 6: The output will be ‘Welcome to your Dashboard!’, as the request is redirected to the index method of DashboardController.