Skip to content

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.5

Attributes (Labmin\Router\Attributes\) ​

AttributeTargetOptions
#[Controller('name', guard: null, providers: [])]ClassRegisters the controller under ?feature=name. Optional class-level Guard and controller-scoped DI providers.
#[Get(name: null, default: false, guard: null)]MethodExposes a GET action (?action=<method> or custom name). default: true handles requests omitting action.
#[Post(name: null, default: false, guard: null)]MethodExposes 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 typeSupplied value
array $dataQuery parameters ($_GET) on GET; body parameters ($_POST or decoded JSON object) on POST.
Labmin\Router\Request $requestRequest object: $request->get($key, $default), $request->post($key, $default), $request->files($key), $request->validationErrors($param).
MyFormDTO $dataValidated #[DataObject]. On failure, controller is skipped and router answers 422 { "errors": { "data": [...] } }.
?MyFormDTO $dataValidated DTO or null, allowing the controller to handle $request->validationErrors('data') itself.

Response classes (Labmin\Router\) ​

Class / HelperPurpose
stringPlain 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\FusionResponseReturned by Fusion::render(...).

Exceptions & Guards ​

  • Labmin\Router\HttpException(int $statusCode, string $message) — Subclass for domain errors (403, 404, 409). LimsRouter rolls back the transaction and returns {"message": "..."} JSON on app requests.
  • Labmin\Router\Guard — Abstract base with abstract public function allows(): bool and optional onDenied(): 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] to new Router(...) / new LimsRouter(...) or #[Controller(..., providers: [...])] for static factories or interface bindings.

Labmin LIMS Module Developer Documentation