sirix / mezzio-routing-contracts
Contracts for sirix/mezzio-routing-attributes route attribute modifiers
Package info
github.com/sirix777/mezzio-routing-contracts
pkg:composer/sirix/mezzio-routing-contracts
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- phpunit/phpunit: ^11.5
README
Contracts for sirix/mezzio-routing-attributes route attribute modifiers.
Pre-1.0 package: Not yet production-ready. Public contracts may change with breaking changes before
1.0.0.
Installation
composer require sirix/mezzio-routing-contracts
Usage
Implement RouteAttributeModifierInterface on any route-related attribute to inject middleware and/or provide default route options.
use Attribute; use Sirix\Mezzio\Routing\Contracts\RouteAttributeModifierInterface; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] final readonly class RequireTenant implements RouteAttributeModifierInterface { public function __construct(private string $tenantHeader = 'x-tenant-id') {} public function getMiddleware(): array { return [RequireTenantMiddleware::class]; } public function getDefaults(): array { return ['tenant_header' => $this->tenantHeader]; } }
Usage with routing attributes:
use Sirix\Mezzio\Routing\Attributes\Attribute\Get; final class OrdersHandler { #[Get('/orders', name: 'orders.list')] #[RequireTenant('x-org-id')] public function list(): ResponseInterface { // ... } }
The routing-attributes package discovers all implementations of RouteAttributeModifierInterface at boot time and merges their middleware and defaults into the route definition.
Interface
interface RouteAttributeModifierInterface { /** @return list<class-string<MiddlewareInterface>|non-empty-string> */ public function getMiddleware(): array; /** @return array<string, mixed> */ public function getDefaults(): array; }