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

More Decks by Enrico Zimuel

Other Decks in Programming


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

    View full-size 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)

    View full-size 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)


    View full-size 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 full-size slide

  5. New Architecture

    View full-size 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

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

    View full-size slide

  7. Example: listener

    View full-size slide

  8. Example: triggering

    View full-size slide

  9. Example: implications

    View full-size slide

  10. MVC is driven by events

    View full-size 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

    Replacement of the Zend\Registry in ZF1

    View full-size slide

  12. Zend\ServiceManager

    The ServiceManager is a Service Locator

    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 full-size 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 full-size 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 full-size 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 full-size slide

  16. Modules

    modules.zendframework.com (>200 modules)

    View full-size slide

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

    – http://modules.zendframework.com

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

    View full-size slide

  18. Thanks!
    Rate this talk at https://joind.in/8051

    View full-size slide