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.
The client address is REMOTE_ADDR, the address of the TCP peer. Client-IP is
never read. X-Forwarded-For is read only when REMOTE_ADDR is one of the
trusted_proxies you configured, from right
to left, skipping trusted proxies. Behind a reverse proxy, list the proxy
there; otherwise every request appears to come from the proxy itself.
Warning:
ip()is still a routing selector, not an access control. With a correcttrusted_proxieslist a client cannot choose the address, but the guarantee is only as good as that list and your proxy: a range that is too wide (or a proxy that passesX-Forwarded-Forthrough without appending the peer it saw) lets a client forge the address again, and an address shared by many users (a NAT, a corporate proxy) identifies all of them. Do not useip()to hide/debug,/internal, health or admin routes. Protect them with authentication middleware or at the network edge (firewall, reverse proxy).
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