Getting Started with Symfony 4 and Flex

Getting Started with Symfony 4 and Flex

With the release of Symfony4, both the architecture and the developer workflow got a major overhaul, mostly by the introduction of Flex as a way to bootstrap your application. The new changes focus on enabling easier cloud deployments & less manual configuration, while supporting both lean microservice setups as well as monolithic applications. I will guide you through these changes to quickly get started with your new projects.

6a1345d8e6dd15b2c78eff0c331963b1?s=128

Denis Brumann

June 07, 2018
Tweet

Transcript

  1. 7.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex Symfony 3.4 Symfony 3.3
 + Additional Features
 + Deprecations LTS Release
 3 years support Symfony 4.0 Symfony 3.3
 + Additional Features
 - Deprecations Standard Release
 8 months support
  2. 8.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex Symfony 4 != Flex You can use Symfony 4 without Flex • Upgrade PHP (>=7.1.3) • Remove deprecations • Upgrade symfony/symfony to ^4.0
  3. 10.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex Symfony Standard Edition Symfony 2/3 applications were based
 on the Symfony Standard Edition (SE). Symfony SE shipped with all components
 and various bundles. Unused functionality is dormant or
 must be removed.
  4. 11.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex Symfony Flex • replaces Symfony Installer (symfony new) • Composer plugin • Autoconfigures bundles • Executes predefined recipes • Removes changes from recipe on uninstall Functionality is downloaded when needed
  5. 12.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex composer create-project \
 symfony/framework-standard-edition:^3.3 \
 my-app
  6. 14.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex composer create-project \
 symfony/skeleton my-app
  7. 16.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex composer create-project \
 symfony/website-skeleton my-app
  8. 18.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── LICENSE ├── README.md ├── app ├── bin ├── composer.json ├── composer.lock ├── phpunit.xml.dist ├── src ├── tests ├── var ├── vendor └── web
  9. 19.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── app ├── bin ├── composer.json ├── composer.lock ├── src ├── var ├── vendor └── web . ├── bin ├── composer.json ├── composer.lock ├── config ├── public ├── src ├── symfony.lock ├── var └── vendor
  10. 20.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── app ├── bin ├── composer.json ├── composer.lock ├── src ├── var ├── vendor └── web . ├── bin ├── composer.json ├── composer.lock ├── config ├── public ├── src ├── symfony.lock ├── var └── vendor
  11. 21.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex dbr:skeleton/ (master*) $ bin/console list Symfony 4.0.9 (kernel: src, env: dev, debug: true) Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -e, --env=ENV The Environment name. [default: "dev"] --no-debug Switches off debug mode. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: about Displays information about the current project help Displays help for a command list Lists commands assets assets:install Installs bundles web assets under a public directory cache cache:clear Clears the cache cache:pool:clear Clears cache pools cache:pool:prune Prunes cache pools cache:warmup Warms up an empty cache config config:dump-reference Dumps the default configuration for an extension debug debug:autowiring Lists classes/interfaces you can use for autowiring debug:config Dumps the current configuration for an extension debug:container Displays current services for an application debug:event-dispatcher Displays configured listeners for an application debug:router Displays current routes for an application lint lint:yaml Lints a file and outputs encountered errors router router:match Helps debug routes by simulating a path info match
  12. 22.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── app ├── bin ├── composer.json ├── composer.lock ├── src ├── var ├── vendor └── web . ├── bin ├── composer.json ├── composer.lock ├── config ├── public ├── src ├── symfony.lock ├── var └── vendor
  13. 23.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex app ├── AppCache.php ├── AppKernel.php ├── Resources │ └── views └── config ├── config.yml ├── config_dev.yml ├── config_prod.yml ├── config_test.yml ├── parameters.yml ├── parameters.yml.dist ├── routing.yml ├── routing_dev.yml ├── security.yml └── services.yml config ├── bundles.php ├── packages │ ├── dev │ │ └── routing.yaml │ ├── framework.yaml │ ├── routing.yaml │ └── test │ └── framework.yaml ├── routes.yaml └── services.yaml
  14. 24.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── app ├── bin ├── composer.json ├── composer.lock ├── src ├── var ├── vendor └── web . ├── bin ├── composer.json ├── composer.lock ├── config ├── public ├── src ├── symfony.lock ├── var └── vendor
  15. 25.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex app_dev.php app.php index.php APP_ENV: dev | prod APP_DEBUG: true | false .env
  16. 26.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── app ├── bin ├── composer.json ├── composer.lock ├── src ├── var ├── vendor └── web . ├── bin ├── composer.json ├── composer.lock ├── config ├── public ├── src ├── symfony.lock ├── var └── vendor
  17. 27.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── SymfonyRequirements.php ├── bootstrap.php.cache ├── cache │ └── dev ├── logs │ └── dev.log └── sessions var ├── cache │ └── dev └── log
  18. 28.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . ├── app ├── bin ├── composer.json ├── composer.lock ├── src ├── var ├── vendor └── web . ├── bin ├── composer.json ├── composer.lock ├── config ├── public ├── src ├── symfony.lock ├── var └── vendor
  19. 29.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex . └── AppBundle ├── AppBundle.php └── Controller └── DefaultController.php . ├── Controller └── Kernel.php formerly
 app/AppKernel (without bundle registration)
  20. 32.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex What is a bundle? Bundles are intended to be reusable components for sharing features between applications. Bundles are Symfony's version of a plugin-system.
  21. 33.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex Migrating a bundled application Move code to App\ and use namespaces for ordering functionality, e.g.
 App\Controller\Backend\IndexController. Install bundles using composer.
  22. 34.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex How to add functionality to a Flex project? Unlike Standard Edition, which came with ready-to-use bundles and default config,
 new components/bundles are added on demand. Flex will automatically enable a bundle in all
 appropriate environments and add necessary
 configuration through recipes & packs.
  23. 35.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex What is a recipe? • Description of things to apply when a package is installed or to be removed on uninstall • maintained by Symfony Core team • stored in Github-repository: symfony/recipes • automatically applied via flex-plugin
  24. 38.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex Packs groups of several packages solving a common problem Recipes registers a bundle with optional configuration, environment variables
 and files
  25. 40.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex What are a contrib-recipes? • maintained by the community • no guaranteed quality standard • open to recipes with certain OS licenses • recipes are not enabled by default
  26. 44.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex services: # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. public: false # Allows optimizing the container by removing unused services; this also means # fetching services directly from the container via $container->get() won't work. # The best practice is to be explicit about your dependencies anyway. # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App\: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests,Kernel.php}' # controllers are imported separately to make sure services can be injected # as action arguments even if you don't extend any base controller class App\Controller\: resource: '../src/Controller' tags: ['controller.service_arguments']
  27. 46.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex _defaults: autowire: true autoconfigure: true public: false
  28. 47.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex App\: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
  29. 50.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex App\Controller\: resource: '../src/Controller' tags: ['controller.service_arguments']
  30. 52.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex App\Meetup\Mailer: arguments: $adminMail: '%env(ADMIN_MAIL)%'
  31. 53.

    Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex _defaults: bind: $adminMail: '%env(ADMIN_MAIL)%'