New in Symfony 5.3: Prototype Options

Contributed by Yonel Ceruto in #39913.

The OptionsResolver component helps you configure objects with option arrays and is used by other components such as Form. In Symfony 5.3 we’ve improved it with prototype options, to resolve and validate a series of options repeatedly as part of another option. Consider a connections option which accepts an array of database connections, each of them defining the same options (host, database, user and password). In Symfony 5.3, the best way to implement this is to use the new setPrototype() method to define the connection options as a prototype that can be repeated: 1 2 3 4 5 6$resolver->setDefault('connections', function (OptionsResolver $connResolver) { $connResolver ->setPrototype(true) ->setRequired(['host', 'database']) ->setDefaults(['user' => 'root', 'password' => null]); });

Thanks to this configuration, you can now pass options like these: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15$resolver->resolve([ 'connections' => [ 'default' => [ 'host' => '127.0.0.1', 'database' => 'symfony', ], 'test' => [ 'host' => '127.0.0.1', 'database' => 'symfony_test', 'user' => 'test', 'password' => 'test', ], // ... ], ]);

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/MgtWWDSnfCQ/new-in-symfony-5-3-prototype-options

Vytvorené 4y | 13. 5. 2021, 7:20:16


Ak chcete pridať komentár, prihláste sa

Ostatné príspevky v tejto skupine

Just one month to go before SymfonyLive Paris 2025 workshops begin!

SymfonyLive Paris 2025, conference in French language only, will already start in 1 month with the workshops! Have a look on the topics and join us! Schedule details are available here.

📣

25. 2. 2025, 15:20:33 | Symfony
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. 2. 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. 2. 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. 2. 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. 2. 2025, 9: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. 2. 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. 2. 2025, 8:40:05 | Symfony