Lesson 8 of 50 – Laravel Named Routes
16%

Laravel Named Routes

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.

1. What Are Named Routes?

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.

2. Create a 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.

3. Generate URLs Using Named Routes

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.

4. Named Routes with Parameters

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.

5. Named Routes with Multiple 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.

6. Redirect Using a Named Route

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.

7. Named Routes in Controllers

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.

8. Route Names with Groups and Checking Routes

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.dashboard
  • admin.students

Generate 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.

9. Practical Assignment

Complete these exercises to practise named routes.

  1. Create a route named home for /home.
  2. Create a route named about for /about.
  3. Generate the URL for the about route using route().
  4. Create a named route called student.show with an ID parameter.
  5. Generate the URL for student ID 105.
  6. Create a named route called course.show with a course parameter.
  7. Redirect a route to the named route home.
  8. Create an admin. route name prefix using a route group.
  9. Run php artisan route:list and check your route names.

Tip: Named routes help you avoid hardcoding URLs throughout your application.


🧠 Quick Quiz

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?