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

MVC + Events + Modules: the new architecture of ZF2

Enrico Zimuel
February 23, 2013

MVC + Events + Modules: the new architecture of ZF2

The new architecture of Zend Framework 2 is based on a new MVC design that uses events and modules. In this talk we will show how to build web applications in ZF2 using the SOLID principle (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion). We will show how to use Events to manage the application flow, how to organize and reuse the code using Modules and how to separate the responsibility using the new MVC design of ZF2.

Enrico Zimuel

February 23, 2013
Tweet

More Decks by Enrico Zimuel

Other Decks in Programming

Transcript

  1. MVC + Event + Module:
    the new architecture of ZF2
    Enrico Zimuel (@ezimuel)
    Software Engineer – Zend Framework Team

    View Slide

  2. About me

    Enrico Zimuel (@ezimuel)

    Software Engineer since 1996

    PHP Engineer at Zend Technologies

    Zend Framework Core Team

    International speaker, author of
    books about secure software and
    web programming

    Co-founder PUG Torino (Italy)
    http://torino.grusp.org

    View Slide

  3. Zend Framework 2

    Open source PHP Framework

    100% OOP, PHP 5.3.3+, Unit tested (PHPUnit)

    New MVC architecture: Event, Service, Module

    New security components (Crypt, Escaper, etc)

    Packaging system (composer, pyrus)

    http://framework.zend.com

    View Slide

  4. SOLID principle

    In ZF2 we followed the SOLID principle
    – Single responsibility, Open-closed, Liskov
    substitution, Interface segregation and
    Dependency inversion

    Some design used:
    – Dependency Injection (Zend\Di)
    – Service Locator (Zend\ServiceLocator)
    – Modular architecture (single components)
    – Extensive use of Interface

    View Slide

  5. New Architecture

    View Slide

  6. Events

    An event is an action (and a value object)

    An event is triggered by an EventManager

    A listener is a callback that can react to an
    event

    An EventManager is an object that holds a
    collection of listeners for one or more named
    events, and which triggers events

    View Slide

  7. Example: listener

    View Slide

  8. Example: triggering

    View Slide

  9. Example: implications

    View Slide

  10. MVC is driven by events

    View Slide

  11. Services

    Services are the “objects” shared in a ZF2
    application (e.g. Db connection)

    Can be everything: class, object, callback

    Managed by a Service Locator
    (Zend\ServiceManager)

    Replacement of the Zend\Registry in ZF1

    View Slide

  12. Zend\ServiceManager

    The ServiceManager is a Service Locator
    implementation

    A Service Locator is a well-known object in
    which you may register objects (more in
    general services) and later retrieve them

    Driven by configuration

    View Slide

  13. Type of Services

    Explicit (name => object pairs)

    Invokables (name => class to instantiate)

    Factories (name => callable returning object)

    Aliases (name => some other name)

    Abstract Factories (unknown services)

    Scoped Containers (limit what can be created)

    Shared (or not; you decide)

    View Slide

  14. Module
    A module is all related code and assets
    that solve a specific problem.
    Modules inform the MVC about services
    and event listeners

    View Slide

  15. Modules in ZF2

    The basic unit in a ZF2 application
    is a Module

    Modules are “Plug and play” technology

    Modules are simple:
    – A namespace
    – Containing a single classfile: Module.php

    View Slide

  16. Modules

    modules.zendframework.com (>200 modules)

    View Slide

  17. DEMO

    View Slide

  18. Where to start?

    Getting started on framework.zend.com
    – http://framework.zend.com/learn

    Free webinar about ZF2
    – http://www.zend.com/en/resources/webinars/framework

    Modules
    – http://modules.zendframework.com

    ZFTool
    – PHAR https://packages.zendframework.com/zftool.phar
    – Github https://github.com/zendframework/ZFTool

    View Slide

  19. Thanks!
    Rate this talk at https://joind.in/8051
    http://framework.zend.com

    View Slide