Features I Like in Symfony 6.3

Martin Mička
3 min readJun 30, 2023

--

Photo by Manuel Nägeli on Unsplash

It has been a month since the release of a new version of my favorite PHP framework. Today, I want to share my thoughts on the features that I enjoy working with in this new version.

Request Payload Mapping

This is probably my favorite feature of this release. Before it was available, we had to implement data mapping ourselves or use a component like Symfony Forms or Serializer to do it for us.

As someone who generally avoids using Symfony Forms for various reasons and finds implementing serializers just for simple data mapping to be overly complicated, I’ve had a difficult time finding a simple solution.

After trying multiple approaches, I found that creating a custom Request class was effective. This class populates data into a desired DTO and runs Symfony Validator against its rules. If there are any errors, an HTTP Exception is raised within the Request class. To reduce code duplication, I used an abstract class that generally runs validation and populates data.

Custom abstract “Request DTO” class

My custom child Request class would act as a data transfer object (DTO) and have its own logic for populating and validating data.

“DTO Request” with custom data population and validation

This request class is then passed as a Controller method argument, “connecting” it to the whole Request-Response lifecycle. If the request data is invalid, it will never pass down to the Controller in the application.

Usage of my “DTO Request” class

That’s still a lot of code for such basic functionality. Fortunately, Symfony 6.3 includes native request data mapping that is much simpler. To use it, we need a DTO with validation constraints:

Basic DTO with some constraint

Now we can pass this DTO with proper attribute to Controller method, like this:

Request Payload Mapping!

Symfony will map Request properties according to our DTO and run the Validator before the execution reaches the Controller. If any errors are present, an exception will be raised.

The MapRequestPayload attribute accepts arguments that modify its default behavior. For example, you can specify which validation groups should be used.

Webhook Component

Another great feature of this framework is its new component for webhooks. Webhooks are commonly implemented without any standards, so having a convenient abstraction for them is a nice improvement.

Although I have not yet tried this component as it is still experimental and lacks documentation, I like the idea of having a standardized way to deal with webhooks.

Dependency Injection Improvements

There are several improvements for dependency injection (DI) within a Symfony application. In my opinion, the most useful ones are enhancements to the Autowire attribute. It now supports nesting other autowiring attributes, such as tagged locators or iterators. Additionally, it can autowire parameters and environment variables via arguments.

Query Parameters Mapper

Another great feature is the ability to map query parameters to controller method arguments via attributes. This feature complements the Request Payload Mapping functionality.

Query Parameter Mapping!

The new release of Symfony includes many great features, and it seems that the web framework is heading in a good direction. Although other frameworks might have had these features for years, it’s still nice to see them being adapted.

--

--

Martin Mička

I'm a working professional in software development, currently working as a Chief Technology Officer of Nelisa.