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. Getting started with
 Symfony 4 & Flex Denis Brumann denis.brumann@sensiolabs.de

    @dbrumann
  2. Denis Brumann Software Developer
 Berlin, Germany denis.brumann@sensiolabs.de @dbrumann

  3. Symfony Versions

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

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

    Flex Maintained Versions 2.8 3.4 4.0 4.1
  6. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex
  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
  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
  9. Bootstrapping a Symfony application

  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.
  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
  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
  13. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex symfony new my-app 3.3
  14. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex composer create-project \
 symfony/skeleton my-app
  15. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

    Flex composer create-project \
 symfony/website-skeleton my-app
  17. New
 Directory Structure

  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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)
  30. Bundle-less
 applications

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

    Flex Your bundles will still work!
  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.
  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.
  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.
  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
  36. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

    Flex
  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
  39. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex
  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
  41. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

    Flex
  43. Dependency Injection Improvements

  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']
  45. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

    Flex _defaults: autowire: true autoconfigure: true public: false
  47. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex App\: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
  48. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

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

    Flex App\Controller\: resource: '../src/Controller' tags: ['controller.service_arguments']
  51. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

    Flex App\Meetup\Mailer: arguments: $adminMail: '%env(ADMIN_MAIL)%'
  53. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

    Flex _defaults: bind: $adminMail: '%env(ADMIN_MAIL)%'
  54. Denis Brumann denis.brumann@sensiolabs.de @dbrumann Getting Started with Symfony 4 &

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

    Flex