What’s a typical Symfony 4.2 application like?

What’s a typical Symfony 4.2 application like?

Symfony's configuration system has improved a lot in recent years. Version 4.2 brings a lot of minor improvements that all together give an even better developer experience. Let's step back and look at how we can tackle all of the most common problems using the new approaches! This will include tricks for configuring services as easily as possible, handling different environment configuration and the hugely important topic of environment variables and secrets. Best of all, we'll discuss some strategies to migrate your existing apps so you're never going to be left behind.

6baa34bc1e5c347b1003f6abe8691de1?s=128

Nicolas Grekas

March 26, 2019
Tweet

Transcript

  1. 9.

    @nicolasgrekas <service id="twig" class="Twig\Environment" public="true"> <argument type="service" id="twig.loader" /> <argument

    /> <!-- Twig options --> <call method="addGlobal"> <argument>app</argument> <argument type="service" id="twig.app_variable" /> </call> <call method="addRuntimeLoader"> <argument type="service" id="twig.runtime_loader" /> </call> </service>
  2. 10.

    services: app.spooky_word_generator: class: App\SpookyWordGenerator app.halloween_costume_creator: class: App\HalloweenCostumeCreator arguments: ['@app.spooky_word_generator'] app.candy_aggregator:

    class: App\CandyAggregator arguments: - '@app.halloween_costume_creator' - '%kernel.project_dir%' app.doorbell_ring_event_subscriber: class: App\DoorbellRingEventSubscriber tags: ['kernel.event_subscriber']
  3. 13.

    @nicolasgrekas Symfony 4 spirit Keep coding features By automating your

    configuration and providing you with great error messages
  4. 16.

    services: _defaults: autowire: true autoconfigure: true public: false bind: string

    $projectRoot: '%kernel.project_dir%' Stricter bindings
  5. 24.

    @weaverryan class DoorbellRingEventSubscriber { private $apiKey; public function __construct(string $apiKey)

    { $this->apiKey = $apiKey; } } services: _defaults: bind: $apiKey: 'ILOVECANDY' Classic problem: avoid hardcoding secret values!
  6. 25.

    @weaverryan services: _defaults: bind: $apiKey: '%env(API_KEY)%' // how the compiled

    code looks $s = new DoorbellRingEventSubscriber(getenv('API_KEY')); API_KEY=ILOVECANDY
  7. 29.

    services: _defaults: bind: $apiKey: '%env(key:API_KEY:json:file:SECRETS_FILE)%' (see EnvProcessor) $secretsFile = getenv('SECRETS_FILE');

    $data = json_decode( file_get_contents($secretsFile) ); $s = new DoorbellRingEventSubscriber( $data['API_KEY'] );
  8. 31.
  9. 32.

    @weaverryan .env .env.local .env.test .env.test.local .env files? commit gitignore commit

    gitignore defaults only local values defaults local values Real env vars are never overridden
  10. 36.
  11. 37.

    $ git commit $ rm symfony.lock $ rm config/ -r

    $ rm src/ -r $ composer sync-recipes $ git diff
  12. 44.

    services: App\MyBlockRender: arguments: - !tagged_locator { tag: block, index_by: block_name

    } App\SomeBlock: tags: { name: block, block_name: some_block } New in 4.3 use !tagged to get an iterator
  13. 45.