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

Symfony in 2019

Symfony in 2019

Fabien Potencier

June 20, 2019
Tweet

More Decks by Fabien Potencier

Other Decks in Programming

Transcript

  1. Fabien Potencier
    @fabpot
    in 2019

    View Slide

  2. How many of are going

    to SymfonyCon Amsterdam?

    View Slide

  3. Join us for a week of Symfony,
    November 19-23, at the Beurs
    van Berlage:
    • November 19-20: 2-day pre-
    conference workshops
    • November 21-22: 2-day
    conference (3 tracks and 1
    Unconference)
    • November 23: 1-day Hackday
    Get an exclusive 10% discount on
    your registration using the code:
    SFC19PHPAMS
    Code available until June 26 2019
    amsterdam2019.symfony.com

    View Slide

  4. Come to meet the
    international Symfony
    and PHP communities!
    Become a Sponsor of the
    event! Support the
    framework and get
    visibility for your
    company during the
    conference!
    Contact us:
    [email protected]
    1,000+ attendees and 30+
    speakers will be there during
    our 2-day conference!

    View Slide

  5. Which

    Symfony version(s)

    are you using?
    1.x? 2.x? 3.x? 4.x?

    View Slide

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

    View Slide



  7. Wired?
    Tired?
    or

    View Slide



  8. Symfony

    Encore
    Webpack
    Assetic

    View Slide



  9. Symfony
    Local Web Server
    Built-in

    PHP server

    View Slide

  10. Symfony Local Web Server
    $ symfony server:start -d
    # open in the browser
    $ symfony open:local
    # tail the logs
    $ symfony server:log

    View Slide

  11. • PHP-FPM (concurrent requests)

    • HTTP 2

    • TLS

    • Local domain names

    • No dependencies / single binary (MacOS, Linux, and Windows)

    • Manages background commands (Encore, Messenger, ...)

    • Consolidated logs (PHP
    , Web server, app, background commands)

    • Docker integration (and SymfonyCloud)

    • Multiple versions of PHP

    • HTTP Link/pre-load/push support
    Symfony
    Local Web Server

    View Slide

  12. 12
    https://symfony.com/download

    View Slide

  13. $ symfony new demo
    $ symfony new demo --cloud --debug
    # Want to try Symfony 5.0 (dev version)?
    $ symfony new demo --version=dev-master
    Creating a new project

    View Slide



  14. Symfony
    CLI
    SensioLabs
    Security
    Checker

    View Slide

  15. $ symfony security:check
    Does not call

    https://security.symfony.com/

    Clones and caches

    https://github.com/FriendsOfPHP/security-advisories/

    View Slide



  16. Symfony

    Components
    symfony/symfony

    View Slide

  17. 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

  18. Symfony 4+/5+
    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

  19. $ symfony new demo
    # Forms are ❌ disabled as symfony/form is not installed
    $ composer req form
    # Forms are automatically ✅ enabled as symfony/form is installed
    + Auto-configuration

    + No manual changes

    + Better performance

    View Slide



  20. Third-party

    Bundles
    with recipes
    Application

    Bundles

    View Slide

  21. $ cat src/Controller/DefaultController.php
    App vs Bundles
    src/ is about YOUR code

    View Slide



  22. Environment
    Variables
    Class Constants
    Parameters n/a

    View Slide

  23. Parameter Class constant Environment variable
    Can be provided

    by a third party ("standard")
    ❌ ❌ ✅
    Can be changed

    without cache:clear
    ❌ ❌ ✅
    Easy to use in code ❌ ✅
    Can be changed

    without redeploy
    ❌ ✅
    Can be different

    in dev and prod
    ✅ ❌ ✅
    Can be stored

    outside of a Git repo
    ✅ ❌ ✅
    Rich type support via processors
    Example
    Configure

    behavior
    Configure

    behavior
    Configure

    infrastructure

    View Slide



  24. Container
    Auto-discovery
    Auto-configuration
    Auto-wiring
    Service

    Configuration

    View Slide

  25. $ composer req twig
    Adding Twig
    + Controller as a service

    + No manual changes

    + No configuration

    + Auto-wiring
    Productivity++

    View Slide

  26. Never define

    your service again*

    View Slide



  27. Maker

    Bundle
    Generator

    Bundle

    View Slide

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

    View Slide

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

    View Slide

  30. $ cat src/Controller/DefaultController.php
    Symfony Maker Bundle
    Annotations
    Everything in one file

    View Slide

  31. 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

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

    View Slide

  33. Full Automation works for…
    • Twig extensions

    • Event listeners

    • Doctrine entities/repositories

    • Commands

    • Voters

    • Registration form system

    • …

    View Slide



  34. Twig

    Templates
    PHP

    Templates

    View Slide

  35. You can use PHP templates
    But Twig benefits

    from a fully maintained integration

    View Slide



  36. Symfony

    Mailer
    Swiftmailer

    View Slide

  37. A great example of

    how we leverage

    the "new" Symfony infrastructure

    View Slide

  38. I want to send emails

    from my controller

    View Slide

  39. $ composer req symfony/mailer
    Sending emails
    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
    + No configuration

    + No manual changes

    + Auto-wiring

    + Env vars

    View Slide

  40. I want to send rich emails

    from my controller

    View Slide

  41. Sending emails
    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'
    ])
    );

    View Slide

  42. I want to send rich and responsive emails

    from my controller

    View Slide

  43. $ composer req twig/inky-extension twig/cssinliner-extension
    Sending emails
    {% 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

  44. I want to send rich and responsive emails

    from my controller

    via my provider's API

    View Slide

  45. $ composer req mailgun-mailer
    MAILER_DSN=http://$MAILGUN_KEY:$MAILGUN_DOMAIN@mailgun
    Sending emails
    Amazon SES
    Google Gmail
    Mandrill
    Mailgun
    Postmark
    Sendgrid

    View Slide

  46. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously

    View Slide

  47. $ composer req messenger
    framework:
    messenger:
    transports:
    email: "doctrine://default"
    routing:
    Symfony\Component\Mailer\Messenger\SendEmailMessage: email
    $ ./bin/console messenger:consume
    $ symfony run -d ./bin/console messenger:consume -vv
    Sending emails
    Go to the background

    View Slide

  48. $ symfony server:status
    $ symfony server:log
    Sending emails

    View Slide

  49. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously with AMQP

    View Slide

  50. At this point, think about

    how you would do it

    with Symfony 3.4

    Here, we have not written

    a single line of code

    and barely changed the configuration

    View Slide

  51. We are only using Symfony components

    View Slide

  52. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously with AMQP

    View Slide

  53. framework:
    messenger:
    transports:
    email: "%env(RABBITMQ_DSN)%"
    routing:
    Symfony\Component\Mailer\Messenger\SendEmailMessage: email
    $ docker-compose up -d
    Sending emails
    rabbitmq:
    image: rabbitmq
    ports:
    - 5672
    docker-compose.yml
    Symfony Local Web Server

    Env vars "auto-wiring"
    Local port is random

    View Slide

  54. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously with AMQP

    and manage failures gracefully

    View Slide

  55. $ composer req symfony/sendgrid-mailer
    MAILER_DSN=http://key:domain@mailgun || http://key@sendgrid
    Sending emails

    View Slide

  56. framework:
    messenger:
    transports:
    email:
    dsn: "%env(RABBITMQ_DSN)%"
    retry_strategy:
    max_retries: 3
    routing:
    Symfony\Component\Mailer\Messenger\SendEmailMessage: email
    Sending emails

    View Slide

  57. framework:
    messenger:
    failure_transport: failed
    transports:
    failed: 'doctrine://default?queue_name=failed'
    email:
    dsn: "%env(RABBITMQ_DSN)%"
    retry_strategy:
    max_retries: 3
    routing:
    Symfony\Component\Mailer\Messenger\SendEmailMessage: email
    $ symfony server:log
    $ ./bin/console messenger:failed:show
    $ ./bin/console messenger:failed:retry
    Sending emails

    View Slide

  58. Sending emails

    View Slide

  59. I want to send rich and responsive emails

    from my controller

    via my provider's API

    asynchronously with AMQP

    and manage failures gracefully

    without Docker

    View Slide

  60. $ symfony project:init
    $ symfony deploy
    $ symfony log --worker=send_emails all
    Sending emails
    rabbitmq:
    type: rabbitmq:3.7
    disk: 1500
    .symfony/services.yaml
    .symfony.cloud.yaml
    runtime:
    extensions: [ "amqp" ]
    relationships:
    rabbitmq: "rabbitmq:rabbitmq"
    workers:
    send_emails:
    commands:
    start: |
    set -x -e
    (>&2 symfony-deploy)
    bin/console messenger:consume -vv email
    Production ready

    View Slide

  61. $ symfony tunnel:open
    Sending emails
    Symfony Local Web Server

    Env vars "auto-wiring"
    .symfony.cloud.yaml
    relationships:
    rabbitmq: "rabbitmq:rabbitmq"
    Development ready
    framework:
    messenger:
    transports:
    email: "%env(RABBITMQ_DSN)%"

    View Slide

  62. Experience
    The new
    Components: http-client, messenger, mailer, container, ...
    Concepts: Flex, auto-configuration, env vars, ...
    Tools: Symfony CLI, SymfonyCloud, Docker

    View Slide

  63. is a fantastic set of low-level building blocks

    that you can assemble to develop

    powerful high-level features easily

    View Slide

  64. What about the future?
    Nobody knows, it depends on you 

    ... so, what have you done so far for Symfony 4.4?

    View Slide

  65. HTTP errors without Twig
    Important for API-only applications

    View Slide

  66. PHP 7.4 preload support
    Performance FTW

    View Slide

  67. A better Security component?
    https://github.com/symfony/symfony/issues/30914

    https://github.com/symfony/symfony/issues/30765

    View Slide

  68. ... to be continued

    View Slide

  69. Thank you and see you soon!

    View Slide