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

Utworzony 4y | 13 maj 2021, 07:20:16


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

22 kwi 2025, 09:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

20 kwi 2025, 08:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

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

16 kwi 2025, 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

15 kwi 2025, 14:50:24 | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

13 kwi 2025, 09:40:02 | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

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

10 kwi 2025, 16:40:34 | Symfony
SymfonyCon Amsterdam 2025: Last days to enjoy early bird tickets!

The international Symfony conference of the year, SymfonyCon Amsterdam 2025, will take place in the Netherlands on November 27 & 28, 2025!

⏳ Early bird registration ends this Wednesday! D

8 kwi 2025, 09:10:13 | Symfony