Download the PHP package a7-tech/laminas-attribute-controller without Composer
On this page you can find all versions of the php package a7-tech/laminas-attribute-controller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download a7-tech/laminas-attribute-controller
More information about a7-tech/laminas-attribute-controller
Files in a7-tech/laminas-attribute-controller
Package laminas-attribute-controller
Short Description Package for Laminas Framework to use attributes sugar in controller.
License proprietary
Informations about the package laminas-attribute-controller
Laminas Attribute Controller
Description
This package provides attribute-based controllers for use in Laminas applications. It simplifies route management, dependency injection, and input handling in controller arguments.
Installation
Requirements
- PHP >= 8.0
- Laminas MVC >= 3.0
- Composer
Instructions
-
Install the package:
- Set up the module in your Laminas application by adding it to the
application.config.php
:
Usage
Example Controller with Attributes
Route Registration
Routes are automatically registered through attributes when the module is enabled.
Attributes Documentation
Route
The Route
attribute defines routing for controller methods.
Parameters:
path
(string): The URL path for the routename
(string|null): Optional name for the routemethods
(array): HTTP methods allowed for this route, defaults to ['GET']
QueryParam
The QueryParam
attribute extracts and validates query parameters from the request.
Parameters:
name
(string): The name of the query parameterconstraints
(array): Array of Symfony validator constraints to applyrequired
(bool): Whether the parameter is required, defaults to false
Autowire
The Autowire
attribute enables automatic dependency injection.
Parameters:
alias
(string|null): Optional service alias for the container, defaults to null (uses the type hint)
IsGranted
The IsGranted
attribute provides auth-based access control for controller methods.
Parameters:
role
(string): The role required to access the method
MapRequestPayload
The MapRequestPayload
attribute maps the request body to a parameter.
This attribute automatically deserializes the request body into the specified type.
Example with Multiple Fields and Validators
Using this DTO with the MapRequestPayload
attribute will automatically validate all fields according to the specified constraints.
MapQueryString
The MapQueryString
attribute maps the query string parameters to a typed object.
This attribute automatically deserializes the query string parameters into the specified type, making it easy to work with structured query parameters in GET requests.
Example with Multiple Fields and Validators
Using this DTO with the MapQueryString
attribute will automatically validate all query parameters according to the specified constraints. This example would handle a query like ?searchTerm=john&limit=50&page=2&sortDirection=desc
.
CurrentUser
The CurrentUser
attribute injects the current authenticated user into a parameter.
For implementation, you need to have a user entity and a service that retrieves the current user from the session or security context implemented LaminasAttributeController\Security\GetCurrentUser
This attribute provides easy access to the authenticated user without manual retrieval.
Advanced Configuration
Custom Resolvers
The package allows you to create and configure your own parameter resolvers. This is useful when you need custom logic for resolving controller action parameters.
To create a custom resolver:
-
Create a class that implements
ParameterResolverInterface
: - Register your resolver in the configuration:
The order of resolvers in the configuration is important as they are tried in sequence until one returns a non-null value.
Default Resolvers
The package comes with the following default resolvers:
FromRouteResolver
: Resolves parameters from route matchesMapRequestPayloadResolver
: Maps request body to a parameter (POST, PUT, PATCH, etc)MapQueryStringResolver
: Maps request to a parameter from query params (GET request)QueryParamResolver
: Extracts and validates query parametersAutowireResolver
: Provides automatic dependency injectionAutoInjectionResolver
: Provides automatic dependency injectionCurrentUserValueResolver
: Injects the current authenticated userDefaultValueResolver
: Uses default parameter values
You can customize which resolvers are used by modifying the configuration:
All versions of laminas-attribute-controller with dependencies
laminas/laminas-mvc Version ^3.0
beberlei/assert Version ^3.3
doctrine/orm Version ^2.0 | ^3.0
jms/serializer Version ^3.30
symfony/validator Version ^5.4 | ^6.0 | ^7.0