Download the PHP package lsbproject/request-bundle without Composer
On this page you can find all versions of the php package lsbproject/request-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lsbproject/request-bundle
More information about lsbproject/request-bundle
Files in lsbproject/request-bundle
Package request-bundle
Short Description Bundle to structure common request data
License MIT
Informations about the package request-bundle
LSBProjectRequestBundle
\ Request bundle created to represent request data as a strict objects.
Installation
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
Usage
Create a request class and extend AbstractRequest:
That's all. This will require foo_bar
parameter to be present in request, query, cookie, head or attribute (All types you can find in RequestStorage
). \
Then use it in controller:
Using objects
Also this bundle supports loading objects like you usually do with @ParamConverter
. All ParamConverts will be performed to the property.
If you are not using PHP 7.4, you can point the class with annotations @var
or @PropConverter
(attributes also available right now with the same annotation class)
(Be aware that you need to specify full classname with a namespace)
Configuring property
As you could notice there is a useful annotation @PropConverter
which is in fact is an adapter to @ParamConverter
of sensio-framework-bundle.
Be free to modify any of parameters, as they are working in the same way as in the original one.
Request storage
By default all parameters from body, request, headers, cookies or path will be used. \
To restrict parameter to be located in exact place you can use @RequestStorage
annotation
From example above you will get foo_baz
parameter from request body or path, and foo_bar
parameter exactly from request body. \
There are 5 types of storage: query
, body
, path
, cookie
and header
.
Validation
You can use symfony/validation
to validate parameters in request.
Install component and use it as usual
Using mutators
To specify property you also can use setters instead of public
properties to add some additional logic.
Working with entities
There is an annotation @Entity
which is almost equal to the sensio annotation.
Use mapping
property to point aliases from the request to the original parameters names.
Custom naming conversion
By default all properties will be converter to snake_case style. You can change this behaviour
by creating a class which implements LSBProject\RequestBundle\Util\NamingConversion\NamingConversionInterface
then you should register it as a service and point it out in the bundle configuration.
You can also apply your conversion to the separate object or its part. Just register your conversion
as a service and point its name in RequestStorage::converter
Using DTOs as property
There is also a possibility to specify deeper nested level in the request. To do it, specify special option of PropConverter::isDto
for class property. This will prevent standart ParamConverter
to be applied and will recursively perform AbstractRequest
converter to the object.
Using Collections
To specify an array of objects which should be converted, use isCollection
property or use typehinting. You can use it in combination \
with isDto
to specify array of objects to be set. \
Be aware that it's not recommended to use collection for entities as it's very inefficient way of getting data as it \
will perform request to DB.
Using discriminator field
Sometimes we do not know which object will be returned. To resolve this discriminator mechanism appears in game.
Correspondingly to field
field in Discriminator
attribute object will be configured accordingly to mapping.
Use on a custom objects
There is also possibility to apply LSB converter to the object
not inheriting AbstractRequest. You can use @LSB\Request
annotation
to point out parameter in controller.
Examples
More examples you can find here
Writing documentation
OpenApi: https://github.com/22116/request-doc-bundle
Known issues
- Multiple types such as a mix of classes and arrays are not supported, but you can still use a mix of scalar types
All versions of request-bundle with dependencies
ext-json Version *
symfony/framework-bundle Version ^3 || ^4 || ^5 || ^6
sensio/framework-extra-bundle Version ^3 || ^4 || ^5 || ^6
symfony/property-info Version ^3 || ^4 || ^5 || ^6