Download the PHP package baptiste-contreras/symfony-request-param-bundle without Composer

On this page you can find all versions of the php package baptiste-contreras/symfony-request-param-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package symfony-request-param-bundle

symfony-request-param-bundle

Goals

This bundle aims at reproduce the Java Spring's Request Param annotation but in PHP with Symfony.

With this bundle you can use PHP 8.1 native attribute to obtain the given result :

In our example,$registerRequest object will be built with the data in the request and validated.

Installation

DtoRequestParam parameters

Several parameters are available for the DtoRequestParam, and it let you modify the behavior of the DTO injection.

sourceType

When you change this value, you must ensure that there is a DtoProviderDriverInterface that can supports that type of sourceType. Otherwise, you will get a NoDtoProviderDriverFoundException

A full description of the packaged sourceType is discussed later in this document.

Example :

throwDeserializationException

If true, any exception during the deserialization phase is not captured and is rethrown. If you turn this parameter to false, exception happening during the deserialization will be captured, logged, and null will be injected instead of the DTO.

Example :

validateDto

If true, a validation phase will be executed, using the Symfony's validator. If there is any contraint violation, the bundle will throw a custom exception and handle the error formatting and display (more on that later)

If false, no validation is done and your DTO will be injected in your controller's method right after the deserialization.

To set up your validation constraints you can use the official Symfony's documentation , but here is a glimpse :

``

Example :

validationGroups

As this bundle use internally the Symfony's validator, we can specify a validation group to only validate a subset of our constraints. You can learn more on that here.

You can pass a single string, meaning only one validation group or an array of string, if you want to use many.

Here is something important to note, if validateDto is true, you can't give an empty array or string ([] or '') or you will get a EmptyValidationGroupsException.

Example :

Sources available

For the moment only the json source is supported. You can extend the bundle by creating a DtoProviderDriverInterface for your needs. Any object that implements this interface will be used internally without any additional action.

When your custom provider's supports method is called and return true, it will be selected for the deserialization.

Here is an example for an XML provider.

You can play with the tag priority of you custom provider if you want to ensure that it's called first. Here is the documentation about that

request_param.dto-provider-driver is the tag associated with the DtoProviderDriverInterface.

An example if you need to modify the priority of your custom provider

JsonDtoProvider

If you specify SourceType::JSON as the sourceType, the JsonDtoProvider service will be used.

Internally it use the Symfony's serializer

For the moment a very basic configuration of the serializer is used, and it populates your DTOs using setters.

Later it will be possible to change that. (Actually you can change that by configuring the serializer component your way, more reading here)

Error Handling

Presenter

An error presenter is a service responsible to return a response in a predefined format. For example json error format :

If you want to add your own error presenter, which return a custom format you just have to create an object that implements ErrorPresenterDriverInterface.

Like the custom provider above, no further actions are required (i.e. if your custom error presenter's supports method returns true, it will be used !).

You must be aware that the first error handler register which returns true will be use. As for the providers, you can play with the tag priority to put your handler first in the selection chain Here is the documentation about that.

request_param.error-presenter-driver is the tag associated with the ErrorPresenterDriverInterface.

An example if you need to modify the priority of your custom provider

When you create your own presenter, in addition of the supports method, you must implement two important methods :

Here is an example of a custom basic HTML error presenter :

JsonErrorPresenter

By default, the JsonErrorPresenter service will be used to returns a JSON response with the error detailed.

Here is an example of 2 responses :

You can easily modify this response format. In fact, this presenter use decorator stacks to create a response array.

Symfony's decorator

Symfony's decorator stack

JsonErrorPresenter::presentBadRequest uses the stack_response_formatter_json_bad_request stack. JsonErrorPresenter::presentTechnicalError uses the stack_response_formatter_json_technical_error stack.

Both stacks above are a sequence of JsonFormatterInterface applied one by one.

Lets look at the stack_response_formatter_json_bad_request definition :

Let's create a new formatter to add a new key, and, lets say, remove the key

We need to add a little more configuration in the services.yaml

And voila

With this decorator approach you can really easily customize the json response

Will produce:

By default, request_param.response.formatter.json.validation is responsible for the errors key and request_param.response.formatter.json.default the others you saw in the above examples.


All versions of symfony-request-param-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
symfony/config Version ~4.4 || ^5.0 || ^6.0
symfony/dependency-injection Version ~4.4 || ^5.0 || ^6.0
sensio/framework-extra-bundle Version ~4.4 || ^5.0 || ^6.0
symfony/http-kernel Version ~4.4 || ^5.0 || ^6.0
symfony/serializer Version ~4.4 || ^5.0 || ^6.0
symfony/serializer-pack Version ^v1.1.0
symfony/validator Version ~4.4 || ^5.0 || ^6.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package baptiste-contreras/symfony-request-param-bundle contains the following files

Loading the files please wait ....