Appearance
labmin/router
Attribute-driven PHP router (^0.5) with query-based ?feature= / ?action= dispatch, automatic constructor DI, Materialize DTO validation, guards, and typed HTTP responses.
bash
composer require labmin/router:^0.5Attributes (Labmin\Router\Attributes\)
| Attribute | Target | Options |
|---|---|---|
#[Controller('name', guard: null, providers: [])] | Class | Registers the controller under ?feature=name. Optional class-level Guard and controller-scoped DI providers. |
#[Get(name: null, default: false, guard: null)] | Method | Exposes a GET action (?action=<method> or custom name). default: true handles requests omitting action. |
#[Post(name: null, default: false, guard: null)] | Method | Exposes a POST action. default: true is independent of #[Get(default: true)]. |
Only GET and POST are supported; other HTTP verbs return 405 Method Not Allowed.
Controller method parameter injection
The router inspects parameter types (order does not matter):
| Parameter type | Supplied value |
|---|---|
array $data | Query parameters ($_GET) on GET; body parameters ($_POST or decoded JSON object) on POST. |
Labmin\Router\Request $request | Request object: $request->get($key, $default), $request->post($key, $default), $request->files($key), $request->validationErrors($param). |
MyFormDTO $data | Validated #[DataObject]. On failure, controller is skipped and router answers 422 { "errors": { "data": [...] } }. |
?MyFormDTO $data | Validated DTO or null, allowing the controller to handle $request->validationErrors('data') itself. |
Response classes (Labmin\Router\)
| Class / Helper | Purpose |
|---|---|
string | Plain HTML response with status 200. |
new HtmlResponse(string $body, int $status = 200, array $headers = []) | HTML with custom status or headers. |
new JsonResponse(mixed $data, int $status = 200, array $headers = []) | JSON response (Content-Type: application/json). |
new FileResponse(string $path, ?string $filename = null, bool $download = true, ?string $contentType = null) | Streams a local file as attachment (download: true) or inline (download: false). |
Router::redirect(string $url, int $status = 303) | RedirectResponse (avoid on Fusion POST actions — return JSON instead). |
new ViewResponse(View $view, int $status = 200) | Renders a PHP object implementing Labmin\Router\View. |
Labmin\Fusion\FusionResponse | Returned by Fusion::render(...). |
Exceptions & Guards
Labmin\Router\HttpException(int $statusCode, string $message)— Subclass for domain errors (403,404,409).LimsRouterrolls back the transaction and returns{"message": "..."}JSON on app requests.Labmin\Router\Guard— Abstract base withabstract public function allows(): booland optionalonDenied(): Response(defaults to HTTP 403). Controller guards run before action guards, before DTO validation or controller construction.
URLs & Dependency Injection
php
// Build relative module URLs (?feature=...&action=...&...)
Router::url(feature: 'orders', action: 'view', query: ['id' => 42]);
// → "?feature=orders&action=view&id=42"- Any concrete class with resolvable constructor dependencies is autowired once per request (request-scoped singleton).
- Pass
providers: [[Factory::class, 'getInstance'], ConcreteForInterface::class]tonew Router(...)/new LimsRouter(...)or#[Controller(..., providers: [...])]for static factories or interface bindings.