New in Symfony 5.3: Configure Multiple Environments in a Single File

Contributed by Nicolas Grekas in #40214 and #40782.

Symfony defines different configuration environments so you can change your application behavior depending on where it’s run (e.g. locally in your development machine, in the production server, etc.) The options applied to bundles/packages in all environments are defined in config/packages/ and the specific options of each environment are defined in config/packages/<environment>/. This works well, but it’s cumbersome when the differences among environments are minimal, because you need to create/maintain another config file just to change a few config options. That’s why in Symfony 5.3 you can also define options for different environments in a single file. The exact syntax to use depends on the format of the config file. In YAML config files, use the when@... special key: 1 2 3 4 5 6 7 8 9 10 11 12 13 14# config/packages/webpack_encore.yaml webpack_encore:

...

output_path: &#39;%kernel.project_dir%/public/build&#39;
strict_mode: true
cache: false

when@prod: webpack_encore: cache: true

when@test: webpack_encore: strict_mode: false

In XML config, wrap the config in the new <when> tag: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19<!-- config/packages/webpack_encore.xml --> <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="..."> <webpack-encore:config> <!-- ... --> </webpack-encore:config>

    &lt;when env=&quot;prod&quot;&gt;
        &lt;webpack-encore:config&gt;
            &lt;!-- ... --&gt;
        &lt;/webpack-encore:config&gt;
    &lt;/when&gt;

    &lt;when env=&quot;test&quot;&gt;
        &lt;webpack-encore:config&gt;
            &lt;!-- ... --&gt;
        &lt;/webpack-encore:config&gt;
    &lt;/when&gt;
&lt;/container&gt;

In PHP config files, use the new env() method to check in which environment is the application running: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15// config/packages/webpack_encore.php use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (FrameworkConfig $framework, ContainerConfigurator $container) { // ...

if (&#39;prod&#39; === $container-&gt;env()) {
    // ...
}

if (&#39;test&#39; === $container-&gt;env()) {
    $framework-&gt;test(true);
    $framework-&gt;session()-&gt;storageFactoryId(&#39;session.storage.mock_file&#39;);
}

};

This syntax also works to define routes and services only in some environments. You can even combine all in a single file to configure some package and create services but only for some environments: 1 2 3 4 5 6 7 8 9 10 11framework: secret: '%env(APP_SECRET)%'

when@dev: services: App\SomeServiceForDev: ~

when@test: framework: test: true

...

The traditional way of using a config file per environment will keep working in the future, but we encourage you to give this new feature a try to reduce the number of config files to maintain. Lastly, classes can now use PHP attributes to tell that they should only be registered as services in some environments: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16use Symfony\Component\DependencyInjection\Attribute\When;

[When(env: 'dev')]

class SomeClass { // ... }

// you can apply more than one attribute to the same class:

[When(env: 'dev')]

[When(env: 'test')]

class AnotherClass { // ... }

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/wnRkgGE-l2E/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file

Utworzony 4y | 7 maj 2021, 07:20:06


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

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
A Week of Symfony #953 (March 31 – April 6, 2025)

This week, the upcoming Symfony 7.3 version entered its feature freeze period to tweak and polish its new features before releasing it at the end of May 2025. In addition, we celebrated the SymfonyLiv

6 kwi 2025, 08:30:10 | Symfony