Download the PHP package webignition/encapsulating-request-resolver-bundle without Composer
On this page you can find all versions of the php package webignition/encapsulating-request-resolver-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webignition/encapsulating-request-resolver-bundle
More information about webignition/encapsulating-request-resolver-bundle
Files in webignition/encapsulating-request-resolver-bundle
Package encapsulating-request-resolver-bundle
Short Description Symfony controller action argument resolver for types that extend Symfony\Component\HttpFoundation\Request
License MIT
Homepage https://github.com/webignition/encapsulating-request-resolver-bundle
Informations about the package encapsulating-request-resolver-bundle
Encapsulating Request Resolver Bundle
Easily inject your own request-encapsulating objects into controller actions.
Tired of injecting the current request into your controller action and then having to poke around in $request->query
or $request->request
for relevant data?
Ever found yourself wanting to model an inbound request in a more domain-specific manner by encapsulating the current request and calling domain-specific methods to get information about the request?
This helps you do that.
Platform requirements
- PHP
>=8.0.2
- Symfony
5.4.* || 6.0.*
Installation
Add as a composer dependency
Register bundle (if not using Symfony Flex)
Usage
Any object implementing EncapsulatingRequestInterface
can be injected as a controller action argument.
EncapsulatingRequestInterface
has a single static create()
method into which is passed the current request. You can do whatever you like with the request.
Example User Creation Scenario
Scenario
Consider an API endpoint for creating a user of an application. The endpoint expects a POST
request having a payload with email
and password
fields. A controller will be ultimately responsible for accessing the relevant aspects of the request and making that information available to relevant services.
Without using this bundle
We can inject the current request into the controller action and poke around in the request payload.
This is a very simplified example. In practice we may want to be checking that both $email
and $password
are not empty.
For more complex requests, validation of the request data prior to processing may be required. It is easy to start to get into territory where request-accessing logic is beyond the responsibilities of the controller.
Even with the more simple of examples, the controller needs to be aware of the shape of the request payload. It could be argued that even this small amount of information is beyond the responsibilities of the controller.
Using this bundle
We can create a custom request class for handling and understanding the data present in a request to create a user. The logic for processing the Symfony request is encapsulated with the custom request class. The custom request class is injected into the controller action.
The more complex or involved your request processing logic is, the more it may make sense to keep that logic under the responsibility of its own class.
All versions of encapsulating-request-resolver-bundle with dependencies
ext-json Version *
symfony/config Version ^6.0
symfony/dependency-injection Version ^6.0
symfony/http-kernel Version ^6.0
symfony/yaml Version ^6.0