Named routes allow you to assign a name to a route and generate its URL using that name. They make your Laravel application easier to maintain when URLs change.
A named route is a route registered with a unique
name using the name() method.
use Illuminate\Support\Facades\Route;
Route::get('/about', function () {
return 'About Us Page';
})->name('about');
Here, about is the route name.
You can use it to generate the URL instead of
writing /about repeatedly.
Generate the URL in PHP:
echo route('about');
The route() helper generates the URL
associated with the named route.
Open routes/web.php and create a route
with a name.
Route::get('/contact', function () {
return 'Contact Soopro Pathshala';
})->name('contact');
You can now generate its URL using:
$url = route('contact');
If your application is running locally, the generated URL will normally be similar to:
http://127.0.0.1:8000/contact
The exact URL depends on your application's configured host and URL settings.
Laravel's route() helper generates a URL
using a route's name.
Route::get('/home', function () {
return 'Home Page';
})->name('home');
Generate the URL:
$url = route('home');
You can use this helper in controllers, Blade views, and other application code.
Example in a Blade view:
<a href="{{ route('home') }}">Go to Home</a>
This generates the correct URL for the route named
home.
Named routes can also contain route parameters.
Route::get('/student/{id}', function ($id) {
return 'Student ID: ' . $id;
})->name('student.show');
Generate a URL for student 101:
$url = route('student.show', ['id' => 101]);
Generated path:
/student/101
The associative array supplies the value for the
{id} parameter.
You can use the same approach when a route has multiple required parameters.
Suppose a route requires both a student ID and a course name.
Route::get('/student/{id}/course/{course}', function ($id, $course) {
return 'Student: ' . $id . ', Course: ' . $course;
})->name('student.course');
Generate its URL:
$url = route('student.course', [
'id' => 101,
'course' => 'PHP'
]);
Generated path:
/student/101/course/PHP
Laravel fills the route placeholders using the supplied parameter values.
You can redirect a user to a named route using
redirect()->route().
Route::get('/dashboard', function () {
return 'Dashboard Page';
})->name('dashboard');
Route::get('/go-dashboard', function () {
return redirect()->route('dashboard');
});
When a visitor opens /go-dashboard,
Laravel redirects them to the route named
dashboard.
This is useful after form submissions, login operations, and other actions that should send users to another page.
Named routes can be used inside controller methods.
Example controller method:
use Illuminate\Http\RedirectResponse;
public function index(): RedirectResponse
{
return redirect()->route('dashboard');
}
This method assumes that a route named
dashboard exists.
You can also generate a URL without redirecting:
$url = route('dashboard');
Generating a URL and redirecting are different:
route() returns a URL, while
redirect()->route() creates a redirect
response.
Route groups can apply a common name prefix to several routes.
Route::name('admin.')->group(function () {
Route::get('/admin/dashboard', function () {
return 'Admin Dashboard';
})->name('dashboard');
Route::get('/admin/students', function () {
return 'Manage Students';
})->name('students');
});
The complete route names are:
admin.dashboardadmin.studentsGenerate the students URL:
$url = route('admin.students');
List registered routes:
php artisan route:list
You can inspect the route names in the command output. Every route name you use must match a registered route name.
Complete these exercises to practise named routes.
home for /home.about for /about.about route using route().student.show with an ID parameter.course.show with a course parameter.home.admin. route name prefix using a route group.php artisan route:list and check your route names.Tip: Named routes help you avoid hardcoding URLs throughout your application.
1. Which method assigns a name to a route?
2. Which helper generates a URL from a route name?
3. How do you redirect to a named route?