In this topic, we will learn about Redirection in Laravel.
let's create a named route with name (i.e profile).
Route::get('/', function () {
return view('redirect_page');
})->name('profile');
Route::get('/redirect', function () {
return redirect()->route('profile');
});
Redirecting to Named Routes
Example
Step 1 − Create a view called redirect_page.php
and save it at resources/views/redirect_page.php
<html> <body> <h1>Example of Redirecting to Named Routes</h1> </body> </html>
Step 2 − In routes.php, we have set up the route for redirect_page.php. We have also set up another route redirect
which will redirect the request to the named route profile.
Step 3 − Visit the following URL to test the named route example.
http://localhost:8000/redirect
Step 4 − After execution of the above URL, you will be redirected to http://localhost:8000/redirect as we are redirecting to the named route profile.
it'll display the html page i.e
Example of Redirecting to Named Routes