Introduction to Zend Framework 2


Published on

Zend Framework has become a standard in the PHP world for web application development and it's successor, Zend Framework 2 is even more powerful. However, there have been many changes architecturally to the new framework that even those who are familiar with ZF1 will find hard to understand initially. In this talk we will explore the fundamental concepts of ZF2, both architecture and intent as we build ourselves a simple application.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to Zend Framework 2

  1. 1. John Coggeshall
  2. 2. Hi! I’m John! • Involved in PHP since circa 1996 • Sr. Architect, Zend Global Services • PHP Core Contributor • ZF Contributor
  3. 3. Introduction to ZF2  In this talk we’re going to look over the key components of any ZF2 application • The Model, View, and Controller architecture • The Module Architecture • Service Manager • Event Manager
  4. 4. Getting Started  The easiest way to get started in ZF2 is to start with the skeleton application: $ composer create-project -sdev -repository-url=“” zendframework/skeleton-application /path/to/install
  5. 5. Basic ZF2 File structure  config/ - Application Config  module/ - Application modules  public/ - Docroot for images, css, etc.
  6. 6. ZF2 Modules  In ZF2 modules are a core concept when developing applications. Everything including the application is a module.  Modules can be application-specific or can be written generically and then loaded into the application via composer
  7. 7. How do Modules work?  Every ZF2 module starts with a Module class which describes the module and the things it provides to the application  Services  Event Handlers  Controllers  Routes  Etc.
  8. 8. How do Modules work?  Modules also have their own configuration files which can setup default values that are later over-written by the application’s configurations.  Useful for creating module-specific routes, or module-specific configurations, etc.  config/module.config.php
  9. 9. How do Modules work?  The module class can implement a number of useful methods  getAutoLoaderConfig() – configure the way classes are autoloaded through this module  getServiceConfig() – set up the way services this module provides can be created and accessed  getModuleDependencies() – Define module dependencies  onBootstrap() – Executed when module is fired up
  10. 10. How are modules structured?
  11. 11. Let’s look at some code
  12. 12. ZF2 MVC  In ZF2, the MVC architecture is entirely driven by the events (ZendMvcMvcEvent)  Bootstrap  Dispatch  Dispatch Error  Finish  Render  Render Error  Route
  13. 13. ZF2 MVC  Typically you don’t have to worry too much about these things, as the basic MVC takes care of things for you  You define routes in the application config which map to controllers / actions  These controller / actions get executed and return a result  This result is passed to the View component to be rendered
  14. 14. ZF2 MVC  Events are useful however because they allow you to augment or short-circuit the default behavior  I.e. Catch the dispatch event and make sure the user is authenticated  I.e. Catch the dispatch error and render error events to do custom logging
  15. 15. Let’s look at some code
  16. 16. Service Manager  MVC, and ZF2 applications in general rely heavily on something called the Service Manager to deal with application dependencies  Examples: The DB adapter used by the application is created by the Service Manager
  17. 17. Service Manager  Implementing dependencies and components as services allows modules to be completely decoupled from each other  Services are identified by unique ID, which is referenced when the service is required  Customization (i.e. a different DB adapter) can be done simply by over-writing the factory associated with that unique ID
  18. 18. Service Manager  In a module services can be defined in various locations  module.config.php (the ‘service_manager’ key)  Module::getServiceConfig() (the programatic approach)
  19. 19. Service Manager  How services can be defined  By factory – identify the key to either a class that implements a Factory interface or other callable which returns the instance  By invokable – Simply identify the class associated with this service  Aliases – Services can have an alias for complicated dependency scenarios
  20. 20. Let’s look at some code
  21. 21. Working with Routes  Routes are one of the first things done in MVC  Goal – Match a given URL to something actionable  Examples  Literal Route (i.e. /exactly/this)  Segment Route (i.e. /articles[/:article_id])  RegEx (i.e. /blog/(?<id>[a-zA-Z0-9_-]+)
  22. 22. Working with Routes  Below is an example simple route
  23. 23. Controllers  When a route is matched, a dispatch event is fired, and the corresponding controller/action is executed  The controller is loaded via service manager (allowing for dependency injection)  Controller action is called and one of two things can be returned  A Response object  An array or instance of the ViewModel class
  24. 24. Let’s look at some code
  25. 25. Summary  This is a very surface-level exploration into the complex possibilities of ZF2, but enough to get started.  Get to know Service Manager and Event Manager very well and they will serve you fantastically, allowing you to write powerful reusable components
  26. 26. Any questions? Rate this talk and provide feedback at Slides will be available at