Upload
john-coggeshall
View
979
Download
4
Embed Size (px)
DESCRIPTION
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.
Citation preview
INTRODUCTION TO ZEND FRAMEWORK 2John 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?
Let’s look at some code
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
Let’s look at some code
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
programatic approach)
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
Let’s look at some code
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
Let’s look at some code
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
THANK YOU!Any questions?
Rate this talk and provide feedback at https://joind.in/10431
Slides will be available at http://www.slideshare.net/coogle