zf2 modules: events, services, and of course, modularity

18

Click here to load reader

Upload: john-coggeshall

Post on 05-Jul-2015

208 views

Category:

Internet


4 download

DESCRIPTION

A presentation I gave at php[world], 2015 in Washington, DC on Zend Framework 2 focusing on the EventManager, ServiceManager, and how to build dependent-free modules for your applications

TRANSCRIPT

Page 1: ZF2 Modules: Events, Services, and of course, modularity

John Coggeshall

Page 2: ZF2 Modules: Events, Services, and of course, modularity

Hi! I’m John!

• Involved in PHP since circa 1996

• Sr. Architect, Zend Global Services

• PHP Core Contributor

•ZF Contributor

Page 3: ZF2 Modules: Events, Services, and of course, modularity

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 4: ZF2 Modules: Events, Services, and of course, modularity

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 5: ZF2 Modules: Events, Services, and of course, modularity

How are modules structured?

Page 6: ZF2 Modules: Events, Services, and of course, modularity

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 7: ZF2 Modules: Events, Services, and of course, modularity

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 8: ZF2 Modules: Events, Services, and of course, modularity

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 9: ZF2 Modules: Events, Services, and of course, modularity

Service Manager

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 10: ZF2 Modules: Events, Services, and of course, modularity

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 11: ZF2 Modules: Events, Services, and of course, modularity

Service Manager

In a module services can be defined in various locations

module.config.php (the ‘service_manager’ key)

Module::getServiceConfig() (the programaticapproach)

Page 12: ZF2 Modules: Events, Services, and of course, modularity

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 13: ZF2 Modules: Events, Services, and of course, modularity

Using Events

All events are managed through the event manager class(es)

Can be automatically injected into your objects via the Service Locator

Implement the EventManagerAwareInterface

Page 14: ZF2 Modules: Events, Services, and of course, modularity

Event Managers

The ZF2 Event manager is the primary way modules should communicate with each other

Optionally, also internally

Broadcast Events for others to react to

I.e. “Dispatch”

Page 15: ZF2 Modules: Events, Services, and of course, modularity

ZF2 Baked in Events

ZF2 uses events throughout its core functionality

Module Manager

Application

Bootstrap, etc.

MVC Events

Routing

Dispatching

Rendering

http://akrabat.com/zend-framework-2/a-list-of-zf2-events/

Page 16: ZF2 Modules: Events, Services, and of course, modularity

Event Scoping

Events are by default scoped to the local object

Each Event Manger is by default unique to that object

If you want to have events of a different scope you have a few options

Provide a way to get your event manager

Use the more global SharedEventManager

Page 17: ZF2 Modules: Events, Services, and of course, modularity

Summary

This is a very surface-level exploration into the complex possibilities of ZF2 modules and events, 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 18: ZF2 Modules: Events, Services, and of course, modularity

Any questions?

Tell me what you think: http://joind.in/11875

Slides will be available at http://www.slideshare.net/coogle