New in Symfony 5.3: Service Autoconfiguration and Attributes

Service Autoconfiguration Using Attributes¶

        Contributed by 
        Nicolas Grekas 
        in #39804.

Service autoconfiguration allows to automatically apply certain configuration to your services, based on your service’s class. This is useful for example to apply certain service tag to classes that implement some interface (e.g. when you create a class implementing Twig\Extension\AbstractExtension, Symfony applies the twig.extension tag to it so you can use it without configuring it). PHP attributes are one of the best new features introduced in PHP 8. They allow to add machine-readable metadata to your code. That’s why in Symfony 5.3 we’ve combined both to allow autoconfigure services using PHP attributes. In previous Symfony versions, you could add the following to your kernel or dependency injection extension: 1 2$container->registerForAutoconfiguration(SomeInterface::class) ->addTag('app.some_tag'); In Symfony 5.3 you can remove this configuration if you like and use instead the

[Autoconfigure] attribute on the PHP interface:

1 2 3 4 5 6 7 8 9 10# src/SomeNamespace/SomeInterface.php namespace App\SomeNamespace; use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;

[Autoconfigure(tags: ['app.some_tag'])]

interface SomeInterface { // ... } In addition to tags, you can autoconfigure other service properties, such as method calls, service configurators, whether the service is public/shared/lazy, etc. Autoconfigurable Attributes¶

        Contributed by 
        Alexander M. Turek
        and Nicolas Grekas
        in #39897,
        #40234
        and #40556.

A different but related feature introduced in Symfony 5.3 allows you to use PHP attributes to autoconfigure instances of classes instead of interfaces. For example, in previous Symfony applications your configuration had to include the following about controllers: 1 2 3 4 5# config/services.yaml App\Controller\: resource: '../src/Controller/' tags: ['controller.service_arguments']

...

This was necessary to apply the controller.service_arguments tag to all the services whose classes where stored in the src/Controller/ directory of your application (this tag allows autowiring the arguments of all controller methods, not only its constructor). In Symfony 5.3 you can remove that configuration if you like and apply instead the following attribute to all your controllers that need that feature: 1 2 3 4 5 6 7 8// src/Controller/MyController.php use Symfony\Component\HttpKernel\Attribute\AsController;

[AsController]

class MyController { // ... } These attribute names follow the pattern #[As...]. Symfony 5.3 provides

[AsCommand] to define a PHP class as a Symfony console command,

[AsEventListener] to define a PHP class as a Symfony event listener, etc.

Note that you don’t need to add these attributes when upgrading to Symfony 5.3. They are an optional way of achieving the same that you get when using autoconfigure: true in config/services.yaml in your current applications.

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/dRcIWlBijYk/new-in-symfony-5-3-service-autoconfiguration-and-attributes

Utworzony 4y | 12 maj 2021, 07:20:05


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24 lut 2025, 16:20:03 | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

24 lut 2025, 13:50:07 | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

23 lut 2025, 10:10:09 | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

21 lut 2025, 09:20:12 | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

20 lut 2025, 10:10:13 | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

19 lut 2025, 08:40:05 | Symfony
SymfonyLive Paris 2025 :  Le Composant Symfony Mapper

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

18 lut 2025, 14:10:24 | Symfony