Symfony 4: request data bundle

This bundle allows you to represent request data in a structured and useful way by creating request data classes.

Almost every request has some input data such as filters and sort parameters for the GET method or request body for the POST method. You always know which data you expect in any action and the basic flow you pass this data through is the next:

  1. Extract input data from the request.
  2. Check its correctness and return validation errors if data is invalid.
  3. Do the business logic using fully safe data.

Let’s see how request-data-bundle can help you to skip the first two steps and do only the business logic in your controller.

Every action has the already validated request data, so you can use it safely. The above controller has the business logic example just to show how you can use the request data objects.

The PaginationRequestData class:

You can notice that the limit and offset type is an integer, but any query parameter comes as a string. This is one more advantage of this bundle, it converts scalar types for you.

The PostRequestData class:

But, where is the validation code? The request-data-bundle dispatches the event you can subscribe to:

I see the following advantages:

  • It reduces the routine in your controller, removes duplicate code and makes it cleaner.
  • It is better to use known data classes in the business logic instead of arrays or separate variables.
  • It does the query parameters type normalization.
  • It helps you avoid the direct data deserialization to the entity which has many disadvantages.
  • It makes unit test coverage easier and more comfortable.
  • Request data classes can be used for auto-generation any documentation like swagger.
  • You can continue…

The nearest update will be supporting xml , yaml , form-data request data formats.

I hope it will be useful for you! Looking forward to your feedback and questions.

The github link: