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

Établi 4y | 13 mai 2021, 07:20:16


Connectez-vous pour ajouter un commentaire

Autres messages de ce groupe

New in Symfony 7.3: Twig Extension Attributes

Contributed by Jérôme Tamarelle in

28 avr. 2025, 08:50:09 | Symfony
A Week of Symfony #956 (April 21–27, 2025)

This week, we kicked off the New in Symfony 7.3 blog series, highlighting all the exciting new features coming in this release. We also unveiled more details about some of the SymfonyOnline June 2025

27 avr. 2025, 09:41:08 | Symfony
SymfonyOnline June 2025: How Doctrine Events Ruined My Day(s)

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

25 avr. 2025, 13:40:15 | Symfony
Introducing A Streaming AMQP Transport for Symfony Messenger

Symfony's Messenger component makes it easy to build message-driven applications. However, developers using symfony/amqp-messenger have long faced a limitation: it relies on polling (get()), which can

25 avr. 2025, 09:10:02 | Symfony
SymfonyLive Berlin 2025: Recap and Replay !

SymfonyLive Berlin 2025 took place just 3 weeks ago!

A huge thank you to everyone who joined us!🔥 The conference brought together the local Symfony community in the heart of Berlin for two da

24 avr. 2025, 14:30:18 | Symfony
New in Symfony 7.3: Global Translation Parameters

Contributed by Hubert Lenoir in

24 avr. 2025, 07:30:24 | Symfony
SymfonyOnline June 2025: FormFlow: Build Stunning Multistep Forms

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

23 avr. 2025, 15:20:21 | Symfony