Lesson 17 of 50 - Learn how to redirect users and display temporary messages.
A redirect sends a user from one URL to another URL. Redirects are commonly used after submitting forms, logging in, logging out, or saving data.
Example:
Route::get('/old-page', function () {
return redirect('/new-page');
});
Route::get('/new-page', function () {
return 'Welcome to the new page!';
});
When a user opens /old-page, Laravel redirects
the browser to /new-page.
Named routes make redirects easier to maintain because you can change a route's URL without changing every redirect that refers to its name.
use Illuminate\Support\Facades\Route;
Route::get('/dashboard', function () {
return 'Welcome to Dashboard';
})->name('dashboard');
Route::get('/go-dashboard', function () {
return redirect()->route('dashboard');
});
The redirect()->route() method redirects
the user to a named route.
Use redirect()->back() when you want to
return the user to the previous page, such as after
processing a form.
Route::post('/save-student', function () {
// Process the submitted data here.
return redirect()->back();
});
Laravel normally uses the previous URL supplied through the request's referrer information. If it is unavailable, the redirect may fall back to the application's root URL.
A flash message is temporary session data that is available during the next request. It is useful for displaying success, error, or warning messages after a form submission.
Example:
Route::get('/save-message', function () {
return redirect('/welcome')
->with('success', 'Data saved successfully!');
});
Route::get('/welcome', function () {
return view('welcome');
});
Laravel stores the success message in the
session for the next request.
Open your Blade file, for example
resources/views/welcome.blade.php,
and display the flash message.
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h2>Welcome to Laravel</h2>
@if (session('success'))
<div style="color: green;">
{{ session('success') }}
</div>
@endif
</body>
</html>
The message appears if the session contains a
success value. Blade's normal
{{ }} syntax escapes the output.
You can use different session keys for different types of messages.
// Success message
return redirect()->back()
->with('success', 'Student added successfully!');
// Error message
return redirect()->back()
->with('error', 'Something went wrong!');
// Warning message
return redirect()->back()
->with('warning', 'Please check your information!');
// Information message
return redirect()->back()
->with('info', 'Your request has been received!');
Display these messages in a Blade view:
@if (session('success'))
<p style="color:green;">{{ session('success') }}</p>
@endif
@if (session('error'))
<p style="color:red;">{{ session('error') }}</p>
@endif
@if (session('warning'))
<p style="color:orange;">{{ session('warning') }}</p>
@endif
@if (session('info'))
<p style="color:blue;">{{ session('info') }}</p>
@endif
You can pass route parameters when redirecting to a named route.
Route::get('/students/{id}', function ($id) {
return 'Student ID: ' . $id;
})->name('students.show');
Route::get('/open-student', function () {
return redirect()->route('students.show', ['id' => 101]);
});
Opening /open-student redirects to
/students/101.
You can also send temporary session data with a redirect:
return redirect()->route('students.show', ['id' => 101])
->with('success', 'Student details opened successfully!');
A common pattern is to process a form and redirect the user to another page with a success message.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:100',
]);
// Save the student using your model here.
return redirect('/students')
->with('success', 'Student added successfully!');
}
}
This example validates the name but does not save a database record. Add your model's create or save logic before redirecting when implementing a real application.
Create a simple Laravel redirect system with the following requirements:
home.Test each route in your browser and confirm that the correct page and message are displayed.
1. Which method redirects to a named route?
2. What is a flash message commonly used for?
3. Which method redirects to the previous page?