Skip to content

Named Routes and URLs

Muhammet Şafak edited this page Jun 9, 2026 · 1 revision

Named Routes & URLs

Naming a route lets you generate its URL later without hard-coding paths.

Naming a route

Use name() on the most recently registered route, or the name option:

$router->get('/users/{id}', 'UserController@show')->name('users.show');

// equivalent:
$router->get('/users/{id}', 'UserController@show', ['name' => 'users.show']);

Names must be unique; a duplicate throws RouterException.

Inside a group, the as option prefixes the names of routes registered in it:

$router->group('/admin', function (Router $route) {
    $route->get('/login', 'AdminController@login')->name('login'); // -> admin.login
}, ['as' => 'admin.']);

Generating URLs with route()

route() returns the URL for a named route. Pass parameter values keyed by name:

$router->route('users.show', ['id' => 42]);
// => http://your-host/users/42
  • Values may be scalars or objects implementing __toString().
  • If no route has the given name, the name is returned unchanged.
$router->get('/articles/{id}/{slug}', 'ArticleController@show')->name('articles.show');

$router->route('articles.show', ['id' => 7, 'slug' => 'hello-world']);
// => http://your-host/articles/7/hello-world

Optional segments

When a named route has optional parameters, unused optional segments are stripped from the generated URL:

$router->get('/admin/{slug}?', 'AdminController@show')->name('admin');

$router->route('admin');                        // => http://your-host/admin/
$router->route('admin', ['slug' => 'reports']); // => http://your-host/admin/reports

Next: Dependency Injection.

Clone this wiki locally