Laravel Named Routes

In this tutorial, we will learn how to name our routes and use them to generate URLs.

Naming a single hard-coded route

In Laravel, we can give names to our routes and use those names when generating the URL for those routes. To name a route, first, we define a route and then chain the name('route_name') method to a function. Now, our web.php file looks like the following:

<?php

use Illuminate\Support\Facades\Route; // import the Route facade

Route::get('/posts', function () { // define a route
    return 'This is a named route.';
})->name('posts'); // and give it a name

The above code defines a hard-coded route with a path of /posts and gives it the name of posts.

Using the named route

To generate the full URL for a named route, we utilize the route('route_name') helper function. The route() helper function generates the full URL for a given named route.

The following Blade template HTML code:

<a href="{{ route('posts') }}">This is a link to the posts route</a>

Will get compiled into the following code on a local server:

<a href="http://127.0.0.1:8000/posts">This is a link to the posts route</a>

On a production server, it becomes similar to:

<a href="http://www.example.com/posts">This is a link to the posts route</a>

In summary, the above route('posts') function produces the http://127.0.0.1:8000/posts URL/text on a local server, and the URL similar to http://www.example.com/posts on a production server.

Naming a single parameter route

To name a route that has a single parameter, in our web.php file, we write:

use Illuminate\Support\Facades\Route; // import the Route facade

// define a single parameter route:
Route::get('/posts/{id}', function (string $id) {
    return 'The post has an id of ' . $id;
})->name('mypost'); // and give it a name

Now, our parametrized route is called mypost.

Note: all the routes in our web app that have the /posts/some_id_value path will be called mypost.

Using the single parameter named route

To generate the full URL of the named, single-parameter route, we also use the route() helper function, but now with two arguments: route('route_name', ['parameter_name' => $parameter_value]).

The first argument is the name of the route, and the second argument is the array that accepts the name of the route's parameter and its value.

Now, in our Blade template HTML code, we can have the following snippet:

<a href="{{ route('mypost', ['id' => 123]) }}">This is a link to the mypost route</a>

Or, if using a local variable as the value of the id parameter:

<a href="{{ route('mypost', ['id' => $some_var]) }}">This is a link to the mypost route</a>

And the above Blade HTML snippet will get compiled to the following HTML code on a local server:

<a href="http://127.0.0.1:8000/posts/123">This is a link to the mypost route</a>

And the following HTML code on a production server:

<a href="http://www.example.com/posts/123">This is a link to the mypost route</a>

Note: The example.com part will be replaced by the actual name of your website.

Naming a multiple parameter route

To define and name a route that has two or more parameters, in a web.php file, we write:

<?php

use Illuminate\Support\Facades\Route; // import the Route facade

// define a two-parameter route:
Route::get('/posts/{id}/{slug}', function (string $id, string $slug) {
    return 'The post has an id of ' . $id . ' and a slug of ' . $slug;
})->name('mypost'); // and give it a name

The above code defines a route that has two parameters called id and slug. Then, by chaining the name('mypost') method to the Route::get() function call, we give our route a mypost name.

Generating a path for the multi-parameter route

To generate the full path/URL for a multi-parameter route, we also use the route() helper function with two arguments. The first argument is the route parameter; the second argument is an array of key value pairs. Each pair represents the parameter's name and a parameter's value.

The blueprint is: route('mypost', ['id' => 123, 'slug' => 'some_slug_value']).

To generate the full path for our named route, in our Blade template file, we can place the following <a href> snippet:

<a href="{{ route('mypost', ['id' => 123, 'slug' => 'some-slug-value']) }}">This is a link to the mypost route</a>

Or, if using some local $myid and $myslug variables:

<a href="{{ route('mypost', ['id' => $myid, 'slug' => $myslug]) }}">This is a link to the mypost route</a>

And it will get compiled to the following code on a local server:

<a href="http://127.0.0.1:8000/posts/123/some-slug-value">This is a link to the mypost route</a>

And to the following code, if we are on a production server:

<a href="http://www.example.com/posts/123/some-slug-value">This is a link to the mypost route</a>

Naming multiple routes

To define and name multiple routes, in our web.php file, we write:

<?php

use Illuminate\Support\Facades\Route; // import the Route facade

Route::get('/', function () { // define a base route
    return 'This is a named home route.';
})->name('home'); // and give it a name

Route::get('/posts', function () { // define a posts route
    return 'This is a named posts route.';
})->name('posts'); // and give it a name

Route::get('/about', function () { // define an about route
    return 'This is a named about route.';
})->name('about'); // and give it a name

This code invokes three different Route::get() function calls to define three different routes/paths. Each Route::get() function call is chained with the name('some_route_name') method, which gives each route a different name.

Generating URLs for multiple named routes

To generate full URLs for the above routes, we use three separate route('route_name') function calls. In an appropriate location in our Blade template file, we can place the following HTML snippets:

<a href="{{ route('home') }}">This is a link to the home route</a>
<a href="{{ route('posts') }}">This is a link to the posts route</a>
<a href="{{ route('about') }}">This is a link to the about route</a>

The above code will get compiled to the following HTML code on a local server:

<a href="http://127.0.0.1:8000">This is a link to the home route</a>
<a href="http://127.0.0.1:8000/posts">This is a link to the posts route</a>
<a href="http://127.0.0.1:8000/about">This is a link to the about route</a>

And the following HTML code on a production server:

<a href="http://www.example.com">This is a link to the home route</a>
<a href="http://www.example.com/posts">This is a link to the posts route</a>
<a href="http://www.example.com/about">This is a link to the about route</a>

Summary

In this tutorial, we learned how to name our routes by chaining the name() method and how to generate full URLs to these routes using the route() helper function.