-
-
Notifications
You must be signed in to change notification settings - Fork 0
Route Groups
Groups apply a shared concern — a path prefix, a domain, a port or a client-IP restriction — to every route registered inside a callback. The callback receives the router.
Groups can be nested, and nested prefixes/name-prefixes accumulate.
$router->group('/admin', function (Router $route) {
$route->get('/', 'AdminController@dashboard'); // /admin/
$route->get('/users', 'AdminController@users'); // /admin/users
});Nested groups combine their prefixes:
$router->group('/admin', function (Router $route) {
$route->group('/posts', function (Router $route) {
$route->get('/', 'PostController@index'); // /admin/posts/
$route->delete('/{id}', 'PostController@delete'); // /admin/posts/{id}
});
});Pass an as option to prefix the names of routes in the group (see
Named Routes & URLs):
$router->group('/admin', function (Router $route) {
$route->get('/login', 'AdminController@login')->name('login'); // name: admin.login
}, ['as' => 'admin.']);Match routes against a host, including host parameters:
$router->domain('{slug}.example.com', function (Router $route) {
$route->get('/', 'TenantController@home');
});A request to https://acme.example.com/ matches and captures acme.
Restrict routes to a specific port:
$router->port(9000, function (Router $route) {
$route->get('/metrics', 'MetricsController@index');
});Only match for requests coming from one or more client IP addresses:
$router->ip('127.0.0.1', function (Router $route) {
$route->get('/debug', 'DebugController@index');
});
$router->ip(['10.0.0.1', '10.0.0.2'], function (Router $route) {
$route->get('/internal', 'InternalController@index');
});Requests from any other address simply do not match these routes, so they fall
through to a 404 unless another route matches. An invalid IP address throws
InvalidArgumentException. See
Upgrading 1.x → 2.0 for how the
client IP is detected.
Groups nest freely and combine with all the other options:
$router->domain('{tenant}.example.com', function (Router $route) {
$route->group('/api', function (Router $route) {
$route->get('/users', 'Api\UserController@index'); // {tenant}.example.com/api/users
}, ['as' => 'api.']);
});Next: Controllers.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration