You can define path parameters when mapping routes.
Any path parameter has two components
- a path component
- a route handler component
Path parameters are wrapped in {}
Path | Parameters Names Detected |
---|---|
/{username} /about |
username |
/{username} /articles/{articleId} |
username , articleId |
/{username} /articles/{articleId} /comments |
username , articleId |
/{username} /articles/{articleId} /comments/{commentId} |
username , articleId , commentId |
... | ... |
Route handlers can inject these parameters by name
// src/api/{username}/articles/{articleId}/comments/{commentId}
return static function(string $username, string $articleId, string $commentId){};
Note
It is important that the variables names match exactly the parameter names.
Route handlers have the authority to define how parameters are matched.
You can configure this behavior using a #[Param]
attribute
// src/api/{username}/articles/{articleId}/comments/{commentId}/get.php
use CatPaw\Web\Attributes\Param;
return static function(
#[Param('\w{3,}')] string $username, // Require $username to be at least 3 characters long.
#[Param('[A-z0-9-]')] string $articleId, // Require $articleId to include only characters from A to z, numbers and '-'
#[Param('[A-z0-9-]')] string $commentId, // Require $articleId to include only characters from A to z, numbers and '-'
){};