Lets adopt symfony 5 in 2020, because Performance matters

Lets adopt symfony 5 in 2020, because Performance matters

Symfony 5.0 released Like Symfony 4.0, it haven't change any directory structure or add any ground- breaking new way of developing. Instead, Symfony 5 builds on top of the symfony 4 and provides new features to make development easier.

Difference between syfmony 5 and Symfony 4.4?

The differences are mainly two:

  • Symfony 4.4 become the LTS version thus replacing the old version 3.4
  • Symfony 5 is allmost same as Symfony 4.4 but its doesn’t contain code that has been marked as deprecated in version 4.4.

Features:

Type validation improvement:
From now on we can specify several types when validation a property using the constraint Type. In this way we can validate if the value given to a property is one of the specified types:
File: User.php

namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;

class Address
{
// ...

/**
* @Assert\Type(type={"alpha", "digit"})
*/

protected $zipCode;
}

In this way, the zipcode property can be either numeric or contain only letters. That is, ABC458 would not valid.

Event Listeners are even easier to declare:
In order to ensure that the Event Listener are as simple to declare as the Event Subscribers, from the version 4.4 Symfony will inspect the type of argument passed to the __invoke method of Event Listner so that it is no longer necessary to specify the type of event that we are listening to in the configuration inside the service.yml file.
That is, if we have the following:

namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;

final class MyDemoListener
{
  public function __invoke(RequestEvent $event): void
  {
    // ...
  }
}

Symfony will know that the declared Event Listener is associated with the Kernal::REQUEST event since the event is of the RequestEvent type.
So, our services.yml file will change from:

# config/services.yaml
services:
 App\EventListener\MyDemoListener:
  tags:
   - { name: kernel.event_listener, event: kernel.request }

to the following:

# config/services.yaml
services:
 App\EventListener\MyDemoListener:
  tags:
   - { name: kernel.event_listener }

WeekType:
WeekType gives us a multitude of options when defining both the type of data and the widget that represent it. For example, if we save this data in arrray format(year, week) and we want to show two selects we can use WeekType as follows:

use Symfony\Component\Form\Extension\Core\Type\WeekType;

$builder->add('DateTime', WeekType::class, [
  'input' => 'array',

  'widget' => 'choice',
]);

Conclusions

  • The improvement of components that are already in stable version with a unified code based on which to work.
  • The refinement of existing functionality by introducing improvements that gradually increase that robustness of the system.

These two points are the best expression of the maturity that the Symfony ecosystem is acquiring and the explanation that many companies are choosing it to carry out their developments.