introduction to zend framework 2
DESCRIPTION
A talk I've given a few times at php|tek and other venues as an introduction to Zend Framework 2.TRANSCRIPT
John Coggeshall
Hi! I’m John!
• Involved in PHP since circa 1996
• Sr. Architect, Zend Global Services
• PHP Core Contributor
•ZF Contributor
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
Getting Started
The easiest way to get started in ZF2 is to start with the skeleton application:
https://github.com/zendframework/zendskeletonapplication
$ composer create-project \
-sdev \
-repository-url=“https://packages.zendframework.com” \
zendframework/skeleton-application \
/path/to/install
Basic ZF2 File structure
config/ - Application Config
module/ - Application modules
public/ - Docroot for images, css, etc.
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
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.
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
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
How are modules structured?
ZF2 MVC
In ZF2, the MVC architecture is entirely driven by the events (Zend\Mvc\MvcEvent)
Bootstrap
Dispatch
Dispatch Error
Finish
Render
Render Error
Route
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
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
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
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
Service Manager
In a module services can be defined in various locations
module.config.php (the ‘service_manager’ key)
Module::getServiceConfig() (the programaticapproach)
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
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_-]+)
Working with Routes
Below is an example simple route
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
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
Any questions?
Slides will be available at http://www.slideshare.net/coogle