26
INTRODUCTION TO ZEND FRAMEWORK 2 John Coggeshall

Introduction to Zend Framework 2

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

Page 1: Introduction to Zend Framework 2

INTRODUCTION TO ZEND FRAMEWORK 2John Coggeshall

Page 2: Introduction to Zend Framework 2

Hi! I’m John!

• Involved in PHP since circa 1996

• Sr. Architect, Zend Global Services

• PHP Core Contributor

• ZF Contributor

Page 3: Introduction to Zend Framework 2

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

Page 4: Introduction to Zend Framework 2

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

Page 5: Introduction to Zend Framework 2

Basic ZF2 File structure

config/ - Application Config

module/ - Application modules

public/ - Docroot for images, css, etc.

Page 6: Introduction to Zend Framework 2

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

Page 7: Introduction to Zend Framework 2

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.

Page 8: Introduction to Zend Framework 2

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

Page 9: Introduction to Zend Framework 2

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

Page 10: Introduction to Zend Framework 2

How are modules structured?

Page 11: Introduction to Zend Framework 2

Let’s look at some code

Page 12: Introduction to Zend Framework 2

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

Page 13: Introduction to Zend Framework 2

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

Page 14: Introduction to Zend Framework 2

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

Page 15: Introduction to Zend Framework 2

Let’s look at some code

Page 16: Introduction to Zend Framework 2

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

Page 17: Introduction to Zend Framework 2

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

Page 18: Introduction to Zend Framework 2

Service Manager

In a module services can be defined in various locations module.config.php (the

‘service_manager’ key) Module::getServiceConfig() (the

programatic approach)

Page 19: Introduction to Zend Framework 2

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

Page 20: Introduction to Zend Framework 2

Let’s look at some code

Page 21: Introduction to Zend Framework 2

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_-]+)

Page 22: Introduction to Zend Framework 2

Working with Routes

Below is an example simple route

Page 23: Introduction to Zend Framework 2

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

Page 24: Introduction to Zend Framework 2

Let’s look at some code

Page 25: Introduction to Zend Framework 2

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

Page 26: Introduction to Zend Framework 2

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