Routes Definition

Basic Route

A basic route is a simple, static path:

$router->route('GET', '/orders', OrderListController::class);

Parameterized Route

To include parameters like a username or ID in the URL:

$router->route('GET', '/orders/{id}', OrderGetController::class);

Regular Expression Constraints

You can add regex constraints to your parameters:

$router->route('GET', '/orders/{id:[0-9]+}', OrderGetController::class);

Optional Parameters

You can define routes with optional segments:

$router->route('GET', '/articles/{id:[0-9]+}[/{slug}]', ArticleGetController::class);

Complex Routes

You can also define more complex routes with multiple parameters:

$router->route('GET', '/course/{year:[0-9]{4}}/{subject:[a-z]+}/{code:[0-9a-f]{4}}', CourseGetController::class);

Enumerated Parameters

For when you have a specific set of acceptable parameter values:

$router->route('GET', '/{language:en|de}/profile', ProfileGetController::class);

Shortcuts

For convenience, Router offers shortcut methods for common HTTP request methods.

$router->get('/orders', 'get_orders_handler');
$router->post('/orders', 'post_orders_handler');
$router->put('/orders/{id}', 'put_order_handler');
$router->patch('/orders/{id}', 'patch_order_handler');
$router->delete('/orders/{id}', 'delete_order_handler');
// ...

You can use these shortcut methods just like you would use the route method, but without the need to specify the HTTP method as the first argument.

The router only facilitates matching HEAD requests to GET routes when a specific HEAD handler is not found. Developers must explicitly ensure that HEAD method calls always return empty response bodies.