Repository navigation
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.
Warning:
domain()is a routing selector, not an access control. The router reads the host from the request'sHostheader, which the client chooses (unless your web server or proxy only serves known host names and rewrites the rest). A request that sendsHost: internal.example.comis routed as that host. Do not use a domain group to keep a route private.
Match routes by the port of the request URL:
// A second entry point that only exists on port 8443 (for example a separate
// listener for a mobile client). Any caller can reach it by asking for that port.
$router->port(8443, function (Router $route) {
$route->get('/mobile/ping', 'MobileController@ping');
});Warning:
port()is not an access control. The port comes from theHostheader (Host: example.com:8443), which the client chooses, not from the port the connection arrived on. A request that sends that header is routed to the group even if your server never listens on that port. Do not put/metrics,/adminor other private routes behindport(),domain()orip(): protect them with authentication middleware or at the network edge (separate listener, firewall, reverse proxy rules).
Only match for requests coming from one or more client IP addresses:
// Pick a variant of a page for known addresses. Anyone who can forge the
// address (see the warning below) merely sees the staff variant, so this must
// never gate anything that matters.
$router->ip('203.0.113.10', function (Router $route) {
$route->get('/banner', 'BannerController@staff');
});
$router->ip(['203.0.113.10', '203.0.113.11'], function (Router $route) {
$route->get('/banner-preview', 'BannerController@preview');
});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.
Warning:
ip()is not an access control. The router takes the client address from theClient-IPheader first, then the first entry ofX-Forwarded-For, and only then fromREMOTE_ADDR($_SERVER['HTTP_CLIENT_IP'],HTTP_X_FORWARDED_FOR,REMOTE_ADDR). It does not check that those headers came from a proxy you trust, so a client that sendsClient-IP: 127.0.0.1is routed as127.0.0.1. Do not useip()to hide/debug,/internal, health or admin routes. Protect them with authentication middleware or at the network edge (firewall, reverse proxy), and have the edge remove or overwriteClient-IPandX-Forwarded-Foron inbound requests so the application never sees a client-supplied value.
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
- Dependency Injection
- Middleware
- Static File Links
- Error Handling
- Caching
- Health Checks
- Readiness Linter
Reference
Migration