-
Notifications
You must be signed in to change notification settings - Fork 0
Routing
Each supported method has a dedicated helper. They all share the signature
(string $path, $handler, array $options = []) and return the router for
chaining.
$router->get('/', $handler);
$router->post('/login', $handler);
$router->put('/users/{id}', $handler);
$router->patch('/users/{id}', $handler);
$router->delete('/users/{id}', $handler);
$router->options('/users', $handler);
$router->head('/health', $handler);
$router->any('/webhook', $handler); // matches every HTTP method$handler may be a closure, a Controller@method string, or an array — see
Controllers.
register() (aliased as add()) registers a route for one or more methods. The
methods can be an array or a |-separated string:
$router->register(['GET', 'POST'], '/search', $handler);
$router->add('GET|POST', '/search', $handler);Supported methods: GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS,
ANY, LINK. An unsupported method throws
InvalidArgumentException.
Most registration methods return $this, so calls chain — including the
modifiers name(), filter()/middleware() and pattern()/where():
$router->get('/articles/{id}', 'ArticleController@show')
->where('id', '[0-9]+')
->name('articles.show')
->middleware(AuthMiddleware::class);name(), filter() and middleware() apply to the most recently registered
route.
When resolving a request, the candidate routes are the routes registered for the
request method, plus all ANY routes, plus all LINK routes (see
Static File Links). When several routes match the same URL,
the most specific one wins — longer paths and more captured parameters rank
higher.
dispatch() performs this resolution and runs the matched route;
resolve(string $method, string $url) returns the matched
Route object (or null) without running it.
getRoutes() returns the registered routes as plain arrays:
$all = $router->getRoutes(); // grouped by HTTP method
$gets = $router->getRoutes('GET'); // only GET routes, keyed by pathNext: Route Parameters.
initphp/router · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Defining Routes
Handling Requests
Reference
Migration