$30 off During Our Annual Pro Sale. View Details »

Symfony 4 in action (updated for 4.3)

Symfony 4 in action (updated for 4.3)

Fabien Potencier

April 26, 2019
Tweet

More Decks by Fabien Potencier

Other Decks in Technology

Transcript

  1. Fabien Potencier
    @fabpot

    View Slide

  2. Have you ever

    used Symfony?
    1.x? 2.x? 3.x? 4.x?

    View Slide

  3. What is Symfony 4?
    A new developer experience
    On a rock solid foundation
    From micro-style to monolith

    View Slide

  4. 4
    in action…

    View Slide

  5. 5

    View Slide

  6. $ symfony new demo
    # Want to try Symfony 4.3 (dev version)?
    $ symfony new demo --version=dev-master

    View Slide

  7. 7

    View Slide

  8. symfony/skeleton
    "require": {
    "php": "^7.1.3",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "symfony/console": "*",
    "symfony/dotenv": "*",
    "symfony/flex": "^1.1",
    "symfony/framework-bundle": "*",
    "symfony/yaml": "*"
    },

    View Slide

  9. $ symfony new demo --version=dev-master --debug
    Demo

    View Slide

  10. $ symfony new demo --version=dev-master --debug
    Demo

    View Slide

  11. $ symfony new demo --version=dev-master --debug
    Demo
    "scripts": {
    "auto-scripts": {
    "cache:clear": "symfony-cmd",
    "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
    "@auto-scripts"
    ],
    "post-update-cmd": [
    "@auto-scripts"
    ]
    },

    View Slide

  12. $ symfony new demo --version=dev-master --debug
    Demo

    View Slide

  13. $ tree -I vendor
    $ find . -type f | wc -l
    Symfony 4 is lightweight
    Only 16 files
    vs 31 SE
    3300 total
    vs 8867 SE
    -65%

    View Slide

  14. New directory structure
    • Less depth, easier to navigate
    • Non-PHP files in their own directories
    (assets/, templates/)
    • Decoupled from Symfony (assets/, src/,
    templates/, public/)

    View Slide

  15. https://github.com/symfony/recipes/

    tree/master/

    symfony/framework-bundle/4.2
    Demo

    View Slide

  16. Recipes repositories
    https://github.com/symfony/recipes

    Curated recipes by Symfony core team

    https://github.com/symfony/recipes-contrib

    Open to all contributions
    370+ recipes

    View Slide

  17. $ composer show | wc -l
    Demo
    Only 25 deps
    vs 38 + 50 = 88 SE

    View Slide

  18. Symfony 2+/3+
    Monolog Bundle
    SwiftMailer Bundle
    Polyfill Util
    Polyfill PHP 7.0
    Polyfill PHP 5.6
    Polyfill Mbstring
    Polyfill Intl ICU
    Polyfill APCu
    PHPUnit Bridge
    Debug Bundle
    Framework Bundle
    Security Bundle
    Twig Bundle
    WebProfiler Bundle
    WebServer Bundle
    Doctrine Bridge
    Monolog Bridge
    Twig Bridge
    Monolog
    Doctrine (11)
    SecurityChecker
    Generator Bundle
    ParamHandler
    FrameworkExtra Bundle
    Distribution Bundle
    Asset
    BrowserKit
    Cache
    ClassLoader
    Config
    Console
    CssSelector
    Debug
    DependencyInjection
    DomCrawler
    Dotenv
    EventDispatcher
    ExpressionLanguage
    Filesystem
    Finder
    Form
    HttpFoundation
    HttpKernel
    Inflector
    Intl
    Ldap
    OptionsResolver
    Process
    PropertyAccess
    PropertyInfo
    Routing
    Security
    Serializer
    Stopwatch
    Templating
    Translation
    Validator
    VarDumper
    WebLink
    Workflow
    Yaml
    4 bridges
    15 librairies
    11 bundles
    42 components
    SwiftMailer

    View Slide

  19. Symfony 4+
    Polyfill Mbstring
    Framework Bundle
    Cache
    Config
    Debug
    DependencyInjection
    Dotenv
    EventDispatcher
    Filesystem
    Finder
    HttpFoundation
    HttpKernel
    Routing
    Yaml
    0 bridges
    1 library
    1 bundle
    13 components
    Flex

    View Slide

  20. $ symfony server:start -d
    $ symfony server:log
    Demo

    View Slide

  21. Demo

    View Slide

  22. $ ./bin/console
    Demo
    15 commands
    vs 57 SE

    View Slide

  23. $ git init
    $ git add .
    $ git commit -a -m"init"
    Demo

    View Slide

  24. $ composer req maker
    Demo

    View Slide

  25. Aliases
    • Shortcuts for common packages (log, behat,
    phpunit, …)
    • Optimized for Symfony Components (no need
    to use the symfony/ prefix, just workflow, dom-crawler, …)
    • Opinionated choices (admin, api, orm, mailer, test,
    …)

    View Slide

  26. Packs
    • Bundle several dependencies into a
    consistent set (debug, profiler, api, test, …)
    $ composer req debug

    View Slide

  27. Discover Aliases and Packs
    https://flex.symfony.com/

    View Slide

  28. $ composer req maker
    $ git diff config/bundles.php
    Symfony Maker Bundle

    View Slide

  29. $ ./bin/console list make
    Symfony Maker Bundle

    View Slide

  30. $ ./bin/console make:controller
    Symfony Maker Bundle

    View Slide

  31. $ composer req annot
    Symfony Maker Bundle

    View Slide

  32. $ ./bin/console make:controller DefaultController
    Symfony Maker Bundle

    View Slide

  33. $ vim src/Controller/DefaultController.php
    Symfony Maker Bundle
    src/ is about YOUR code

    View Slide

  34. Symfony 4 helps you

    grow your app

    with ease

    View Slide

  35. $ composer req twig
    Adding Twig

    View Slide

  36. Never define

    your service again*

    View Slide

  37. Adding a Twig Extension
    Where do I store
    the Twig extension
    class?
    Which file do I
    need to change to
    register it?
    Which tag do I need
    to use?
    How do I declare a
    tag in YAML?
    Which interface/class
    do I need to implement/
    extend? It’s too complex. What
    about doing it the dirty
    way and do it in the
    controller instead?
    hmm, ok

    View Slide

  38. $ ./bin/console make:twig-extension
    Adding a Twig Extension
    Add your logic in the
    generated class…
    DONE!

    View Slide

  39. Full Automation works for…
    • Twig extensions
    • Event listeners
    • Doctrine repositories
    • Commands
    • Voters
    • Registration form system
    • …

    View Slide

  40. Beyond “Hello World”
    • Defining some models via Doctrine
    • Adding an API for my model
    • Managing data with an admin

    View Slide

  41. $ composer req orm admin api
    Full stack really quick

    View Slide

  42. $ composer req orm admin api
    Full stack really quick

    View Slide

  43. $ ./bin/console make:entity Product --api-resource
    Full stack really quick

    View Slide

  44. $ vim config/packages/easy_admin.yaml
    Full stack really quick

    View Slide

  45. $ vim .env
    DATABASE_URL="sqlite:///%kernel.project_dir%/var/
    data.db"
    $ ./bin/console doctrine:database:create
    $ ./bin/console doctrine:schema:update --force
    Full stack really quick

    View Slide

  46. It works!
    One line config change
    One annotation added

    View Slide

  47. View Slide

  48. Typical features
    to the next level

    View Slide

  49. I want to send emails

    from my controller

    View Slide

  50. $ composer req symfony/mailer
    Symfony Maker Bundle
    use Symfony\Component\Mailer\MailerInterface;
    public function index(MailerInterface $mailer)
    {
    $mailer->send(
    (new Email())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Some subject')
    ->text('Some text...')
    );
    MAILER_DSN=smtp://localhost

    View Slide

  51. I want to send rich emails

    from my controller

    View Slide

  52. Symfony Maker Bundle
    use Symfony\Component\Mailer\MailerInterface;
    public function index(MailerInterface $mailer)
    {
    $mailer->send(
    (new TemplatedEmail())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Some subject')
    ->htmlTemplate('@emails/welcome.html.twig')
    ->context([
    'city' => 'Tunis'
    ])
    );
    MAILER_DSN=smtp://localhost

    View Slide

  53. I want to send rich and responsive emails

    from my controller

    View Slide

  54. $ composer req twig/inky-extension
    Symfony Maker Bundle
    MAILER_DSN=smtp://localhost
    {% apply inky|inline_css(source("@zurb/stylesheets/main.css")) %}




    Symfony Connect







    Forgot Your Password?

    It happens. Click the link below to reset it.
    Reset Password

    unsubscribe here.



    {% endapply %}

    View Slide

  55. I want to send rich and responsive emails

    from my controller

    via my provider's API

    View Slide

  56. $ composer req symfony/mailgun-mailer
    MAILER_DSN=http://key:domain@mailgun
    Symfony Maker Bundle
    Amazon SES
    Google Gmail
    Mandrill
    Mailgun
    Postmark
    Sendgrid

    View Slide

  57. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously

    View Slide

  58. $ composer req messenger
    framework:
    messenger:
    transports:
    email: "doctrine://default"
    routing:
    Symfony\Component\Mailer\Messenger\SendEmailMessage: email
    $ ./bin/console messenger:consume
    Symfony Maker Bundle

    View Slide

  59. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously with AMQP

    View Slide

  60. $ composer req messenger
    framework:
    messenger:
    transports:
    email: "amqp://guest:guest@localhost:5672/%2f/messages"
    routing:
    Symfony\Component\Mailer\Messenger\SendEmailMessage: email
    $ ./bin/console messenger:consume
    Symfony Maker Bundle

    View Slide

  61. Symfony 4 recap
    • Auto-configuration for any Composer package
    • A Maker bundle to simplify creating files
    • No boilerplate code, just business logic
    • Controllers and commands as services
    • From micro to monolith
    • Meta-packages doing a lot for you out of the box
    Productivity
    boost!
    Better DX

    View Slide

  62. Why?

    View Slide

  63. Featureful and the most downloaded

    View Slide

  64. The most advanced API solution
    https://api-platform.com/

    View Slide

  65. The most advanced e-commerce solution

    View Slide

  66. Many admin-gen solutions
    https://github.com/EasyCorp/EasyAdminBundle

    View Slide

  67. The foundation of the PHP ecosystem

    View Slide

  68. $ composer require symfony/console
    Truly independent components
    (new Console\Application('echo', '1.0.0'))
    ->register('echo')
    ->addArgument('foo', Console\Input\InputArgument::OPTIONAL, 'The directory')
    ->addOption('bar', null, Console\Input\InputOption::VALUE_REQUIRED)
    ->setCode(function(Console\Input\InputInterface $input,
    Console\Output\OutputInterface $output) {
    // output arguments and options
    })
    ->getApplication()
    ->setDefaultCommand('echo', true) // Single command application
    ->run();

    View Slide

  69. • CssSelector
    • Debug
    • Dotenv
    • Finder
    • Mailer
    • Mime
    • sMessenger
    • Polyfill : APCu, ctype, iconv, intl, mbstring, PHP, …
    • Process
    • VarDumper
    • VarExporter
    • Yaml
    • …
    Truly independent components

    View Slide

  70. Predictable releases and support
    https://symfony.com/releases
    •Every month: a patch release
    •Every 6 month: a minor release
    •Every 2 years: a major release
    •On the go: security releases

    View Slide

  71. Predictable releases and support
    Release
    end of
    support
    +8 months +14 months +36 months +48 months
    end of security
    support
    end of security
    support
    end of
    support
    Standard Releases
    (3.3, 4.0)
    LTS Releases
    (2.8, 3.4)

    View Slide

  72. Predictable releases and support
    3.4
    LTS
    4.2
    Stable
    https://symfony.com/releases

    View Slide

  73. Stability and predictability
    • Strict semantic versioning
    • Deprecation/BC policy
    • Responsible security issue management
    https://symfony.com/bc
    https://symfony.com/security

    View Slide

  74. The fastest framework around
    x2
    x3
    x4
    http://www.phpbenchmarks.com/

    View Slide

  75. Scale from micro to monolith
    13 files
    15 dependencies
    start with
    … and grow as you see fit
    via automation

    View Slide

  76. Community oriented
    • Code of conduct

    https://symfony.com/conduct
    • Enforced by the CARE team

    https://symfony.com/care
    • Respectful Review Comments

    https://symfony.com/doc/current/contributing/community/review-comments.html
    Diversity initiative

    https://symfony.com/diversity

    View Slide

  77. https://symfony.com/4

    View Slide

  78. Thank you!

    View Slide