Properties
ARoute instance contains the following information:
- URL: The path template for the endpoint (e.g.,
/api/v1/users/{id}). - Method: An implementation of
MethodInterfacerepresenting the allowed HTTP verb (GET, POST, etc.). - Description: An optional human-readable description used for documentation (OpenAPI).
- Tags: A collection of
TagObjectinstances used for grouping endpoints in documentation. - Security Requirements: A
RouteAuthorizationinstance defining the authentication and authorization needed for the route. - Rate Limit: An optional
RateLimitInterfaceimplementation defining the rate limiting rules for the endpoint.
Path Parameters
Routes support dynamic path parameters using the{parameterName} syntax. For example:
Security and Authorization
Security requirements are defined on a per-route basis using theRouteAuthorization object. This allows you to specify which authentication scheme (e.g., Bearer, OAuth2) is required and what granular scopes and permissions the user must have.
Basic Security Example
To make a route private, you add aRouteAuthorization to the route’s constructor:
Public and Optional Security
- Public Route: If no security requirements are provided (default), the route is public.
- Optional Security: Currently, if you want a route to be public but also optionally use identity information if provided, you leave
RouteAuthorizationasnull. TheSecurityMiddlewareonly enforces authorization if aRouteAuthorizationis defined.
Integration with Documentation
TheRoute class implements JsonSerializable, allowing it to be easily exported for OpenAPI (Swagger) generation. It includes helper methods for:
jsonSerialize(): Converts the route and its metadata into a format suitable for JSON export.static byJson(string $json): Hydrates aRouteobject from a JSON string, which is used when loading routes from the router cache.
Usage in Controllers
In an Apivalk application, you typically don’t instantiateRoute objects manually. Instead, you define them in your controller’s static getRoute() method:
Rate Limiting
You can add rate limiting to a route by providing a rate limit object as the last argument to theRoute constructor: