-
-
Notifications
You must be signed in to change notification settings - Fork 0
Named Routes and URLs
Muhammet Şafak edited this page Jun 9, 2026
·
1 revision
Naming a route lets you generate its URL later without hard-coding paths.
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.']);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-worldWhen 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/reportsNext: Dependency Injection.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration