Download the PHP package arimac/laravel-request-mapper without Composer
On this page you can find all versions of the php package arimac/laravel-request-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-request-mapper
Request Mapper for Laravel
This component allow you to inject DTO object mapped from the Request to the action.
- Install
- Requirements
- Basic usage
- Nested object
- Mapped strategies
- Create custom mapped strategy
- How to create an custom exception?
- Project example
- Contributing
- Licence
- TODO
You can install this package via composer using this command:
The package will automatically register itself.
PHP 7.1 or newer and Laravel 5.5 or newer
3.1 Create an DTO object
Your DTO object should extend one of RequestData classes:
- AllRequestData
- HeaderRequestData
- JsonRequestData
RequestData classes responsible for mapped strategies.
$data
array in the init
it is an array
which return from the mapped strategies classes. Basically $data
it is some data from the Request
.
3.2 Inject to the action
DTO object can be injected to any type of action.
3.3 Validate DTO object
You can apply validation to the DTO object:
- before mapping data to the DTO (
laravel
validation) - after mapping data to the DTO (
symfony annotation
validation)
3.3.1 Apply laravel validation
Laravel validation applied for the RequestData
object before object filling.
-
You should create a class with validation rules. This class should implement
Maksi\LaravelRequestMapper\Validation\BeforeType\Laravel\ValidationRuleInterface
interface (in case, if you do no need change the validationmessages
andcustomAttributes
, than you can extendMaksi\LaravelRequestMapper\Validation\BeforeType\Laravel\AbstractValidationRule
class) - In the next you should apply this rules to the DTO object. This should be done via
annotation
.
string
indicates that \Maksi\LaravelRequestMapper\Tests\Integration\LaravelNestedValidation\Stub\ValidatorRule
rules for the data
which will be injected to the dto.
3.3.2 Apply symfony annotation validation
Annotation symfony validation applied to the properties in the RequestData
object (So this validation appied after the creating and DTO object).
At the first you should add the @Type(type="annotation")
annotation to the RequestData object. After this you can apply the validation to the DTO object (for more information please see symfony validation documentation)
4.1. Symfony annotation validation
In the same way you can create an nested DTO object, for example:
Root class
Nested class
4.2. Laravel validation for nested
So, as a laravel validation applied before filling the RequestData
object, than you should just create the same validation class as an for no nested validation.
By default package has 3 strategies:
- AllStrategy
- HeaderStrategy
- JsonStrategy
AllStrategy - responsible for filling data from the $request->all()
array. If you want to use this strategy, than your RequestData
object should extend AllRequestData
class.
HeaderStrategy - responsible for filling data from the $request->header->all()
array. If you want to use this strategy, than your RequestData
object should extend HeaderRequestData
class.
JsonStrategy - responsible for filling data from the $request->json()->all()
array. If you want to use this strategy, than your RequestData
object should extend JsonRequestData
class.
6. Create custom mapped strategy
You can create a custom mapped strategies for our application.
6.1 Create custom strategy
You strategy should implement StrategyInterface;
Method support
define is strategy available for resolve
object. This method has 2 parameters $request
and $object
:
$request
as aRequest
instance$object
- it is empty DTO instance, witch will be filled
Method resolve
will return the array which will be injected to the DTO instance. This method accept $request
object.
6.2 Create RequestData class for Strategy
You should extend RequestData in case if you want to create your own strategy
6.3 Register your strategy in the ServiceProvider
You should add instance of your strategy
to the Maksi\LaravelRequestMapper\StrategiesHandler
via addStrategy
method.
7. Change validation exception
- Create Exception which will extend
\Maksi\LaravelRequestMapper\Validation\ResponseException\AbstractException
and implement toResponse method
For example:
- Define in
config/laravel-request-mapper.php
exception-class
key
You can see example of usage part of this package in https://github.com/E-ZSTU/rozklad-rest-api project.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
- [ ] think about https://www.reddit.com/r/laravel/comments/af843q/laravel_request_mapper/edx39cj
- [ ] think about https://www.reddit.com/r/laravel/comments/af843q/laravel_request_mapper/edx8mci
- [ ] delete symfony validation, since I`m not sure that is needed for the laravel community
- [ ] add integration tests for
change exception
- [ ] add priority to the strategies
- [ ] how you can get this DTO from the middleware (just register
RequestData
as a singleton)
All versions of laravel-request-mapper with dependencies
doctrine/cache Version ^1.0
illuminate/contracts Version ^8.0
illuminate/http Version ^8.0
illuminate/support Version ^8.0
illuminate/validation Version ^8.0
symfony/validator Version ^5.4