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

Symfony 4 & Symfony Flex - Core ideas, features and improvements

Symfony 4 & Symfony Flex - Core ideas, features and improvements

Since its release back in 2005, Symfony has been continuously evolving and improving. With every new version, the framework has pushed forward, helping us writing better code faster.
In this talk we will see the new features and improvements that Symfony 4 is going to introduce next November. We will learn the core ideas from this new version, understanding not only how to use it, but also what are the key assumptions and motivations behind.
Moreover, we will have a close look at the so-called “Symfony 4 secret weapon”: Symfony Flex. This Composer plugin has been created with the idea of cutting configuration times to the minimum. We will understand how it works and learn how to use it properly to boost our productivity.

Victoria Quirante

October 24, 2017
Tweet

More Decks by Victoria Quirante

Other Decks in Programming

Transcript

  1. Symfony 4 & Symfony Flex
    Core ideas, features and improvements
    Victoria Quirante

    View Slide

  2. I work at Limenius
    We build tailor-made projects with
    Symfony and React
    We have been working with Symfony
    since 2008
    SF4 means a significant evolution Victoria Quirante
    @vicqr
    [email protected]

    View Slide

  3. SYMFONY 4 MOTIVATION
    What are the driving
    forces behind this new
    version
    https://www.flickr.com/photos/gsfc/14411386334

    View Slide

  4. SYMFONY 4 MOTIVATION
    Or why the hell
    do we have to change
    things again
    https://www.flickr.com/photos/mindaugasdanys/3766009204

    View Slide

  5. «As a developer, I want to start small, without too many dependencies. But I also
    want to be able to grow my application as I see fit. From a micro-framework style app
    to a gigantic monolith. Your choice. The framework should not get in the way.»
    Fabien Potencier
    «Symfony Flex is all about making it simple and easy to create any Symfony applications, from
    the most simple micro-style project to the more complex ones with dozens of dependencies. It
    automates adding and removing bundles. It takes care of giving you sensible defaults. It helps
    discovering good bundles.»
    Fabien Potencier

    View Slide

  6. Victoria Quirante
    @vicqr
    [email protected]
    SF 4 seems like the result of a SWOT analysis
    Victoria Quirante
    @vicqr
    [email protected]
    S W
    O T
    Flexible
    Powerful
    Config time high
    Learning curve
    SF 2
    Web development
    environment

    View Slide

  7. Victoria Quirante
    @vicqr
    [email protected]
    Recent evolution of the web development environment
    Our favourite
    framework
    Some jQuery
    {
    {
    Back-end
    Front-end

    View Slide

  8. Victoria Quirante
    @vicqr
    [email protected]
    Recent evolution of the web development environment
    Our favourite
    framework
    Some jQuery
    {
    {
    Back-end
    Front-end
    Backbone.js Ember.js Angular.js
    Gulp Grunt
    Vue.js Webpack React.js
    chats notifications immediate
    validations
    page reloads

    View Slide

  9. Victoria Quirante
    @vicqr
    [email protected]
    Our favourite
    framework
    Some jQuery
    Recent evolution of the web development environment
    {
    {
    Back-end
    Front-end
    Our favourite
    front-end
    framework
    Our favourite
    back-end
    framework
    API
    Backbone.js Ember.js Angular.js
    Gulp Grunt
    Vue.js Webpack React.js
    chats notifications immediate
    validations
    page reloads

    View Slide

  10. Victoria Quirante
    @vicqr
    [email protected]
    Our favourite
    framework
    Some jQuery
    Recent evolution of the web development environment
    {
    {
    Back-end
    Front-end
    Our favourite
    front-end
    framework
    Our favourite
    back-end
    framework
    API
    Our favourite
    front-end
    framework
    API

    View Slide

  11. Victoria Quirante
    @vicqr
    [email protected]
    Our favourite
    framework
    Some jQuery
    Recent evolution of the web development environment
    {
    {
    Back-end
    Front-end
    Our favourite
    front-end
    framework
    Our favourite
    back-end
    framework
    API
    Our favourite
    front-end
    framework
    API
    API
    API
    API
    API

    View Slide

  12. Then, what is expected
    from a back-end
    framework nowadays?

    View Slide

  13. Victoria Quirante
    @vicqr
    [email protected]
    - To be a great solution for APIs
    - To make easy to build some functionalities (admin panels, etc.)
    - To be efficient creating small apps and microservices
    - ...
    What is expected nowadays from a back-end framework?
    To take good care of the back-end

    View Slide

  14. Victoria Quirante
    @vicqr
    [email protected]
    SF 4 seems like the result of a SWOT analysis
    S W
    O T
    Flexible
    Powerful
    Config times high
    Learning curve
    APIs everywhere
    Microservices needed
    Backends needed
    Drupal/Laravel
    Frontend revolution
    SF 2
    Web development
    environment

    View Slide

  15. In front of this, what
    things should we
    change?

    View Slide

  16. SYMFONY 4 PHILOSOPHY
    https://www.flickr.com/photos/[email protected]/16357411958

    View Slide

  17. Victoria Quirante
    @vicqr
    [email protected]
    Symfony roadmap

    View Slide

  18. Victoria Quirante
    @vicqr
    [email protected]
    Symfony 2 was two things
    1. A full stack framework
    1. A full stack framework
    2. A set of independent
    components

    View Slide

  19. Victoria Quirante
    @vicqr
    [email protected]
    Symfony 2 was two things
    1. A full stack framework
    But “developing in Symfony 2” generally meant using the
    full framework

    View Slide

  20. Victoria Quirante
    @vicqr
    [email protected]
    Example: If you wanted to build a console app before
    You could start with the full FW and get many things you don’t need
    Or go for the components approach… and fight quite a bit

    View Slide

  21. Victoria Quirante
    @vicqr
    [email protected]
    Symfony 2 VS Symfony 4 graphically
    SF 2
    web app
    SF 2
    API
    SF 2
    microservice

    View Slide

  22. Victoria Quirante
    @vicqr
    [email protected]
    Symfony 2 VS Symfony 4 graphically
    SF 2
    web app
    SF 2
    API
    SF 2
    microservice
    SF 4
    web app
    SF 4
    API
    SF 4
    microservice

    View Slide

  23. Victoria Quirante
    @vicqr
    [email protected]
    Working with SF 4
    SF 4
    web app
    SF 4
    API
    SF 4
    microservice
    SF 4

    View Slide

  24. SF4 is not a new FW, it is
    a new mindset behind
    the same FW

    View Slide

  25. Victoria Quirante
    @vicqr
    [email protected]
    Symfony 4 - Compose your application
    Make easy to create projects with lots or few dependencies
    For that, they wanted automate bundle installation/configuration

    View Slide

  26. It is key that we can
    make our project grow
    or shrink very easily

    View Slide

  27. Victoria Quirante
    @vicqr
    [email protected]
    Working with SF 4
    SF 4
    web app
    SF 4
    API
    SF 4
    microservice
    SF 4
    We need something to smooth
    out this transformations

    View Slide

  28. SYMFONY FLEX
    What exactly is it

    View Slide

  29. SF FLEX is not a version of
    SF, it is a tool to implement
    SF4’s philosophy

    View Slide

  30. Victoria Quirante
    @vicqr
    [email protected]
    Symfony Flex - Automation to the max
    It is a composer plugin that comes with Symfony 4
    Moving towards zero configuration time
    It also “promotes” a curated list of official recipes

    View Slide

  31. Victoria Quirante
    @vicqr
    [email protected]
    Internals
    Modifies the behaviour of the require and update commands
    Allows Symfony to perform tasks before or after the Composer commands

    View Slide

  32. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    composer require mailer
    It installs and enables
    SwiftmailerBundle

    View Slide

  33. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    composer req mailer
    It installs and enables
    SwiftmailerBundle

    View Slide

  34. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer

    View Slide

  35. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Symfony Flex
    server
    Is there information
    about this package?

    View Slide

  36. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Is there information
    about this package?
    Going for regular
    installation via Composer
    Nope
    Symfony Flex
    server

    View Slide

  37. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Is there information
    about this package?
    Symfony Flex
    server

    View Slide

  38. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Is there information
    about this package?
    Yes, sending recipe
    Cool, following recipe
    instructions!
    Symfony Flex
    server

    View Slide

  39. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Is there information
    about this package?
    Yes, sending recipe
    Cool, following recipe
    instructions!
    Deciding which packages to install
    Symfony Flex
    server

    View Slide

  40. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Is there information
    about this package?
    Asking Composer to
    install them
    Yes, sending recipe
    Cool, following recipe
    instructions!
    Deciding which packages to install
    Symfony Flex
    server

    View Slide

  41. Victoria Quirante
    @vicqr
    [email protected]
    How it works
    Your application with
    Symfony Flex
    composer require mailer
    Is there information
    about this package?
    Asking Composer to
    install them
    Yes, sending recipe
    Cool, following recipe
    instructions!
    Deciding which packages to install
    Running any tasks required
    Symfony Flex
    server

    View Slide

  42. Victoria Quirante
    @vicqr
    [email protected]
    Recipes
    Defined in a manifest.json file
    May contain other files or directories
    It ‘explains’ Symfony Flex what things need to be done

    View Slide

  43. Victoria Quirante
    @vicqr
    [email protected]
    Recipes
    {
    "bundles": {
    "Symfony\\Bundle\\SwiftmailerBundle\\SwiftmailerBundle": ["all"]
    },
    "copy-from-recipe": {
    "config/": "%CONFIG_DIR%/"
    },
    "env": {
    "MAILER_URL": "smtp://localhost:25?encryption=&auth_mode="
    },
    "aliases": ["mailer", "mail"]
    }

    View Slide

  44. Victoria Quirante
    @vicqr
    [email protected]
    Recipes
    {
    "bundles": {
    "Symfony\\Bundle\\SwiftmailerBundle\\SwiftmailerBundle": ["all"]
    },
    "copy-from-recipe": {
    "config/": "%CONFIG_DIR%/"
    },
    "env": {
    "MAILER_URL": "smtp://localhost:25?encryption=&auth_mode="
    },
    "aliases": ["mailer", "mail"]
    }
    Environments that this bundle
    has to enable automatically

    View Slide

  45. Victoria Quirante
    @vicqr
    [email protected]
    Recipes
    {
    "bundles": {
    "Symfony\\Bundle\\SwiftmailerBundle\\SwiftmailerBundle": ["all"]
    },
    "copy-from-recipe": {
    "config/": "%CONFIG_DIR%/"
    },
    "env": {
    "MAILER_URL": "smtp://localhost:25?encryption=&auth_mode="
    },
    "aliases": ["mailer", "mail"]
    }
    Files and directories that will be
    copied into your application

    View Slide

  46. Victoria Quirante
    @vicqr
    [email protected]
    Recipes
    {
    "bundles": {
    "Symfony\\Bundle\\SwiftmailerBundle\\SwiftmailerBundle": ["all"]
    },
    "copy-from-recipe": {
    "config/": "%CONFIG_DIR%/"
    },
    "env": {
    "MAILER_URL": "smtp://localhost:25?encryption=&auth_mode="
    },
    "aliases": ["mailer", "mail"]
    }
    New environment variables that
    will be added to your application

    View Slide

  47. Victoria Quirante
    @vicqr
    [email protected]
    Recipes
    List of alternative shorter names
    for the package
    {
    "bundles": {
    "Symfony\\Bundle\\SwiftmailerBundle\\SwiftmailerBundle": ["all"]
    },
    "copy-from-recipe": {
    "config/": "%CONFIG_DIR%/"
    },
    "env": {
    "MAILER_URL": "smtp://localhost:25?encryption=&auth_mode="
    },
    "aliases": ["mailer", "mail"]
    }

    View Slide

  48. Victoria Quirante
    @vicqr
    [email protected]
    Official and unofficial recipes
    Two repositories: core and contrib
    official repository
    (very) opinionated
    approval from SF core team
    Symfony Flex looks here
    aliases
    core
    community repository
    not opinionated
    -
    Symfony Flex may look here
    -
    contrib

    View Slide

  49. Victoria Quirante
    @vicqr
    [email protected]
    Creating a new Symfony app with Symfony Flex
    composer create-project symfony/skeleton my-project

    View Slide

  50. Victoria Quirante
    @vicqr
    [email protected]
    Creating a new Symfony app with Symfony Flex
    composer create-project symfony/skeleton my-project
    "require": {
    "php": "^7.0.8",
    "symfony/flex": "^1.0",
    "symfony/console": "^4.0",
    "symfony/framework-bundle": "^4.0",
    "symfony/lts": "^[email protected]",
    "symfony/yaml": "^4.0"
    },
    Downloads the Symfony skeleton
    (just one composer.json)

    View Slide

  51. Victoria Quirante
    @vicqr
    [email protected]
    Creating a new Symfony app with Symfony Flex
    composer create-project symfony/skeleton my-project
    "require": {
    "php": "^7.0.8",
    "symfony/flex": "^1.0",
    "symfony/console": "^4.0",
    "symfony/framework-bundle": "^4.0",
    "symfony/lts": "^[email protected]",
    "symfony/yaml": "^4.0"
    },
    Downloads the Symfony skeleton
    (just one composer.json)
    composer install
    symfony/flex is the first package installed
    And runs

    View Slide

  52. Victoria Quirante
    @vicqr
    [email protected]
    May I avoid using Symfony Flex?
    Yes, you can install/configure things as you used to
    But Symfony Flex is the default way of doing things in Symfony 4

    View Slide

  53. Victoria Quirante
    @vicqr
    [email protected]
    May I use Symfony Flex with an old project?
    Yes, but installing Symfony Flex is not enough
    You need to update the directory structure
    https://symfony.com/doc/master/setup/flex.html#upgrading-existing-applications-to-flex

    View Slide

  54. WHAT HAS CHANGED
    Things we have to ‘relearn’
    https://www.flickr.com/photos/vialbost/37167061631

    View Slide

  55. Do you remember those
    never-ending discussions
    about one VS multiple bundle
    apps?

    View Slide

  56. Guess what was the right
    answer…

    View Slide

  57. «[...] We worked hard during the last few months to make sure that Symfony 4 can
    embrace bundle-less applications. So Symfony 4 will recommend and generate
    bundle-less applications.»
    Fabien Potencier

    View Slide

  58. View Slide

  59. «[...] We worked hard during the last few months to make sure that Symfony 4 can
    embrace bundle-less applications. So Symfony 4 will recommend and generate
    bundle-less applications.»
    Fabien Potencier
    «No more bundle for your code, just use App\ as a namespace for any class under
    src/. It reduces the perceived complexity. It also makes your code feel more
    decoupled from Symfony.»
    Fabien Potencier

    View Slide

  60. Victoria Quirante
    @vicqr
    [email protected]
    Bundle-less applications
    Change in best practices
    No more one-bundle-application
    Now zero-bundle-application is recommended
    In practice, not difficult to get
    used to this

    View Slide

  61. Victoria Quirante
    @vicqr
    [email protected]
    Directory structure
    my-project/
    ├── config/
    │ ├── bundles.php
    │ ├── packages/
    │ ├── routes.yaml
    │ └── services.yaml
    ├── public/
    │ └── index.php
    ├── src/
    │ ├── ...
    │ └── Kernel.php
    ├── templates/
    └── vendor/
    It may be a bit tricky at the beginning,
    but it is actually simpler

    View Slide

  62. Victoria Quirante
    @vicqr
    [email protected]
    config/bundles.php
    File where installed bundles get referenced
    Symfony Flex is going to write here

    View Slide

  63. Victoria Quirante
    @vicqr
    [email protected]
    config.yml -> packages/package-name.yaml
    Forget about the single long config file
    Now our configuration is splitted in different files
    One file per package/bundle and environment

    View Slide

  64. Victoria Quirante
    @vicqr
    [email protected]
    parameters.yml -> Environment variables
    Now we have .env
    More standard way of managing settings
    # .env
    APP_ENV=dev
    APP_DEBUG=1
    APP_SECRET=d5b7eb4ea25adca8c17bsdsc126831c
    DATABASE_URL="mysql://travolta:[email protected]:3306/travolta"#

    View Slide

  65. Victoria Quirante
    @vicqr
    [email protected]
    Unified web front controller
    No more app.php and app_dev.php
    Just one (public/index.php)
    Trickier to switch between them
    But it is considered a better practice
    # public/index.php
    $kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? false);

    View Slide

  66. WHAT IS NEW
    What new shiny things
    do we get?
    https://www.flickr.com/photos/blondinrikard/14419731227

    View Slide

  67. Of course, Symfony Flex,
    but what else?

    View Slide

  68. Victoria Quirante
    @vicqr
    [email protected]
    Some key things already introduced in SF 3
    - Automated configuration:
    - Autowiring
    - _defaults, _instanceof
    - Autoconfiguration
    - Further standardization:
    - PSR-4 based service discovery
    - PSR-11 containers
    - ...
    https://symfony.com/blog

    View Slide

  69. Victoria Quirante
    @vicqr
    [email protected]
    Many not too flashy but yet powerful additions
    - Improvements in:
    - Guard authentication
    - Forms
    - Injection of tag services
    - Console exceptions
    - Overriding templates
    - ...
    - Integration with Bootstrap 4 form theme
    - ...
    https://symfony.com/blog

    View Slide

  70. AND, finally, just when we
    had lost almost all hope...

    View Slide

  71. A great solution
    for the assets

    View Slide

  72. Victoria Quirante
    @vicqr
    [email protected]
    Webpack Encore
    It simplifies the integration of our Symfony project with Webpack
    Webpack is a great tool to process the assets
    Configuring Webpack is not easy
    But Webpack Encore solves that

    View Slide

  73. DEMO TIME
    Let’s create a project
    with Symfony Flex
    https://www.flickr.com/photos/callmewhatever/30708918322

    View Slide

  74. Victoria Quirante
    @vicqr
    [email protected]
    Let’s create a project with Symfony Flex
    We are going to create a project with API Platform and EasyAdmin

    View Slide

  75. Victoria Quirante
    @vicqr
    [email protected]
    API Platform

    View Slide

  76. Victoria Quirante
    @vicqr
    [email protected]
    EasyAdmin

    View Slide

  77. Victoria Quirante
    @vicqr
    [email protected]
    composer create-project symfony/skeleton apiadmin-project
    cd apiadmin-project
    composer req api
    composer req admin
    composer req webserver
    - Create Entity
    - Config database
    bin/console doctrine:schema:update
    - Tell API Platform and EasyAdmin about our Entity
    bin/console server:run
    Demo time
    Let’s do it

    View Slide

  78. EVALUATING
    How good are these changes?

    View Slide

  79. Victoria Quirante
    @vicqr
    [email protected]
    Fair questions about SF 4 & SF Flex
    1. Is it more difficult now to develop a ‘standard web project’?
    2. Trying to make config extra simple… is it getting more obscure?
    3. Are ‘official packages’ too promoted?
    Still early to know

    View Slide

  80. Victoria Quirante
    @vicqr
    [email protected]
    The good
    It makes sense not to carry dependencies that you are not using
    The things we need to adapt to are not difficult
    Moving further towards standardization

    View Slide

  81. Victoria Quirante
    @vicqr
    [email protected]
    The real good
    Trying to adapt to the upcoming challenges
    Webpack Encore
    Working together with the front-end,
    not fighting against it

    View Slide

  82. BET 1: Let’s embrace the position of the back-end framework in
    the landscape, and try to adapt to our time
    BET 2: Let’s keep our Symfony as powerful and flexible, while
    trying to compete against more high-level-oriented solutions

    View Slide

  83. Victoria Quirante
    @vicqr
    [email protected]
    Fears and hopes
    Great FW to create
    fully tailored-made
    apps
    SF
    It tries to gain ground on
    more high-level-oriented
    solutions
    It remains as good as always
    (or better) for that
    While making easier to build
    some “standard” stuff
    Optimistic outcome
    Pessimistic outcome
    It loses focus
    It loses love from traditional
    SF developers

    View Slide

  84. FINAL THOUGHTS
    What to remember
    https://www.flickr.com/photos/dewalick/15842170328

    View Slide

  85. Victoria Quirante
    @vicqr
    [email protected]
    To summarize
    S W
    O T
    Flexible
    Powerful
    Config times high
    Learning curve
    APIs everywhere
    Microservices needed
    Backends needed
    Drupal/Laravel
    Frontend revolution
    SF 2
    Web development
    environment

    View Slide

  86. Victoria Quirante
    @vicqr
    [email protected]
    To summarize
    S W
    O T
    Flexible
    Powerful
    Config times high
    Learning curve
    APIs everywhere
    Microservices needed
    Backends needed
    Drupal/Laravel
    Frontend revolution
    SF 2
    Web development
    environment

    View Slide

  87. Victoria Quirante
    @vicqr
    [email protected]
    To summarize
    S W
    O T
    Flexible
    Powerful
    Config times high
    Learning curve
    APIs everywhere
    Microservices needed
    Backends needed
    Drupal/Laravel
    Frontend revolution
    SF 2
    Web development
    environment

    View Slide

  88. Victoria Quirante
    @vicqr
    [email protected]
    To summarize
    S W
    O T
    Flexible
    Powerful
    Config times high
    Learning curve
    APIs everywhere
    Microservices needed
    Backends needed
    Drupal/Laravel
    Frontend revolution
    SF 2
    Web development
    environment

    View Slide

  89. Victoria Quirante
    @vicqr
    [email protected]
    To summarize
    Clear answer to the current times
    Imho, the direction taken looks promising
    There may be some risks, but it depends on how will it evolve
    The community has, of course, a lot to say in this

    View Slide

  90. Thanks!
    Victoria Quirante
    @vicqr
    [email protected]

    View Slide