Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Nicolas Grekas

March 26, 2019
Tweet

More Decks by Nicolas Grekas

Other Decks in Technology

Transcript

  1. @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. 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. @nicolasgrekas Symfony 4 spirit Keep coding features By automating your

    configuration and providing you with great error messages
  4. services: _defaults: autowire: true autoconfigure: true public: false bind: string

    $projectRoot: '%kernel.project_dir%' Stricter bindings
  5. @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. @weaverryan services: _defaults: bind: $apiKey: '%env(API_KEY)%' // how the compiled

    code looks $s = new DoorbellRingEventSubscriber(getenv('API_KEY')); API_KEY=ILOVECANDY
  7. 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. @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
  9. $ git commit $ rm symfony.lock $ rm config/ -r

    $ rm src/ -r $ composer sync-recipes $ git diff
  10. 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