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

Zend Framework 2 and Symfony2: The Perfect Team (PHPDay 2013)

Zend Framework 2 and Symfony2: The Perfect Team (PHPDay 2013)

The next generation of frameworks is upon us, and now that they're more decoupled and component-based than ever before and use the same standard for naming and autoloading, there is no reason to stick to a single framework for your projects. During this session, we will go through several ways of combining Zend Framework 2 and Symfony2, so that you walk away being able to focus even more on on writing the complex custom logic your project needs, and don't have to worry about the rest.

Stefan Koopmanschap

May 18, 2013
Tweet

More Decks by Stefan Koopmanschap

Other Decks in Technology

Transcript

  1. Zend Framework 2 and Symfony2:
    Zend Framework 2 and Symfony2:
    The Perfect Team
    The Perfect Team
    Stefan Koopmanschap, Enrico Zimuel
    18th May – Verona (Italy)

    View full-size slide

  2. About us

    Stefan Koopmanschap

    Enterpreneur: Ingewikkeld and
    Techademy

    Member of Dutch Web Alliance

    Symfony Community Manager

    Co-Founder PHPBenelux, now PFZ.nl

    [email protected]

    Enrico Zimuel

    Software Engineer at Zend
    Technologies

    Zend Framework Core Team

    Co-founder PUG Turin

    [email protected]

    View full-size slide

  3. PHP frameworks

    Symfony and Zend Framework are the most
    used PHP frameworks worldwide

    Both offer a tons of features that can improve
    the development of web applications

    Modular architectures and high quality PHP
    code (SoC + SOLID principles)

    Why don't use it together to get the best
    features of both?

    View full-size slide

  4. Religion war?
    ZF sucks!
    Symfony?
    Oh My!

    View full-size slide

  5. Instead of think about
    ZF2 || Symfony 2
    think about
    ZF2 && Symfony 2

    View full-size slide

  6. PSR-*

    Framework interoperability standards

    Meant to make it easier to work with multiple
    frameworks

    Naming standards and coding standards

    For instance: Only one autoloader!

    http://www.php-fig.org/

    View full-size slide

  7. PSR-0

    Autoloading standard:
    \\(\)*
    Each namespace separator is converted to a
    DIRECTORY_SEPARATOR when loading from the file
    system

    View full-size slide

  8. PSR-1

    Basic coding standard:
    Files MUST use only Namespaces and classes MUST follow PSR-0
    Class constants MUST be declared in all upper case
    with underscore separators
    Method names MUST be declared in camelCase
    ...

    View full-size slide

  9. PSR-2

    Coding style guide:
    Code MUST follow PSR-1
    Code MUST use 4 spaces for indenting, not tabs
    There MUST be one blank line after the namespace
    declaration, and there MUST be one blank line after the
    block of use declarations
    Opening braces for classes MUST go on the next line,
    and closing braces MUST go on the next line after the
    body
    ...

    View full-size slide

  10. PSR-3

    Logger interface:
    The LoggerInterface exposes eight methods to write
    logs to the eight RFC 5424 levels (debug, info, notice,
    warning, error, critical, alert, emergency)
    A ninth method, log, accepts a log level as first
    argument. Calling this method with one of the log level
    constants MUST have the same result as calling the
    level-specific method
    ...

    View full-size slide

  11. Composer

    Composer (getcomposer.org) can help the
    management of projects that use ZF2 and
    Symfony2
    – Select your favorite ZF2 and Symfony2
    components using composer.json
    – $ php composer.phar install
    – Components installed in /vendor
    – Include the /vendor/autoload.php in your PHP
    code, that's it!

    View full-size slide

  12. How to integrate
    How to integrate
    Zend Framework 2 in Symfony 2
    Zend Framework 2 in Symfony 2

    View full-size slide

  13. composer create-project
    symfony/framework-standard-edition
    project

    View full-size slide

  14. Composer.json
    "repositories": [
    {
    "type": "composer",
    "url": "http://packages.zendframework.com/"
    }
    ],

    View full-size slide

  15. Composer.json
    "require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.2.*",
    (...)
    "zendframework/zend-crypt": "2.2.*"
    },

    View full-size slide

  16. composer update

    View full-size slide

  17. Now let's get to work

    View full-size slide

  18. Register Crypt class as service

    View full-size slide

  19. src/ZendCon/CryptBundle/Resources/config/services.x
    ml

    View full-size slide

  20. src/ZendCon/CryptBundle/Controller/CryptController.p
    hp
    src/ZendCon/CryptBundle/Resources/views/Crypt/index.html.tw
    ig

    View full-size slide

  21. http://zendcon.local/cry
    pt

    View full-size slide

  22. src/ZendCon/CryptBundle/Controller/CryptController.p
    hp
    src/ZendCon/CryptBundle/Resources/views/Crypt/check.html.tw
    ig

    View full-size slide

  23. http://zendcon.local/cry
    pt

    View full-size slide

  24. Other (better?) solutions

    Do validation in Entity

    Use Symfony2 Validator

    View full-size slide

  25. src/ZendCon/CryptBundle/Controller/CryptController.p
    hp
    src/ZendCon/CryptBundle/Resources/views/Crypt/blockcipher.html.tw
    ig

    View full-size slide

  26. http://zendcon.local/crypt/block

    View full-size slide

  27. Some stuff I won't show...

    Zend\Barcode

    Zend\Captcha

    Zend\Feed

    Zend\I18n

    ZendService_*

    View full-size slide

  28. How to integrate
    How to integrate
    Symfony2 in Zend Framework 2
    Symfony2 in Zend Framework 2

    View full-size slide

  29. How to use Symfony2 in ZF2

    Use composer.json to include the Symfony2
    components

    Execute the update (or install) command of
    composer

    The Symfony2 composer will be installed in /vendor

    MVC approach:

    Register the Symfony2 components in the
    ServiceManager

    “Standard” approach:

    Use the Symfony2 components directly
    (instantiate the class and use it)

    View full-size slide

  30. composer.json
    {
    "require": {
    "php": ">=5.3.3",
    "zendframework/zendframework": "2.0.*",
    "symfony/yaml": "2.2.*",
    "symfony/dom-crawler": "2.2.*",
    },
    "autoload": {
    "psr-0": {
    "Application": "module/Application/src"
    }
    }
    }

    View full-size slide

  31. Symfony component as invokable
    // a module config "module/SomeModule/config/module.config.php"
    return array(
    'service_manager' => array(
    'invokables' => array(
    // Keys are the service names
    // Values are valid class names to instantiate
    'YamlParser' => 'Symfony\Component\Yaml\Parser',
    'DomCrawler' => 'Symfony\Component\DomCrawler\Crawler',
    // ...
    ),
    )
    );

    View full-size slide

  32. Symfony component as factory
    // a module config "module/SomeModule/config/module.config.php"
    return array(
    'service_manager' => array(
    'factories' => array(
    // Keys are the service names.
    // Valid values include names of classes implementing
    // FactoryInterface, instances of classes implementing
    // FactoryInterface, or any PHP callbacks
    'YamlParser' => 'Symfony\Component\Yaml\Parser',
    'DomCrawler' => function ($html) {
    return new Symfony\Component\DomCrawler\Crawle($html)
    }
    // ...
    ),
    )
    );

    View full-size slide

  33. Use a service in a Controller
    class AlbumController extends AbstractActionController
    {
    public function indexAction()
    {
    $sm = $this->getServiceLocator();
    $yamlParser = $sm->get('YamlParser');
    $domCrawler = $sm->get('DomCrawler');
    }
    // ...
    }

    View full-size slide

  34. Symfony 2 components

    Some components useful for ZF2 users:
    – BrowserKit (simulates a web browser)
    – CssSelector
    – Filesystem
    – Finder (file search)
    – OptionsResolver
    – Process (executes commands in sub-processes)
    – Serializer (handle serializing data structures)
    – YAML parser

    View full-size slide

  35. Concluding
    Concluding

    View full-size slide

  36. To integrate

    PSR / Framework Interoperability

    Composer

    Pick your components

    Don't stop at Zend Framework/Symfony

    Don't Reinvent The Wheel!

    View full-size slide

  37. Where to go?
    http://packagist.org

    View full-size slide

  38. Thanks!
    http://framework.zend.com
    Give feedback:
    https://joind.in/8645
    http://www.symfony.com

    View full-size slide