Zend Framework 2 Intro

24
Zend Framework 2 An introduction

description

A zend framework 2 tutorial slides

Transcript of Zend Framework 2 Intro

Zend Framework 2An introduction

ZF2 Overview1. MVC Overview2. Service Manager3. Event Manager4. Example

MVC Overview

...

Key Features● Module based = Everything is in a module

○ Community contributed modules: http://modules.zendframework.com/

● MVC is Event driven and uses a Service Manager

● PSR-0 compliant, uses namespaces

ZF1 vs ZF2Some key differences:1. modularized structure - a module should only do one thing and do it well.2. configurations are PHP Array base.3. dependencies are installed in the VENDOR directory.4. PHP namespaces5. no more Zend_Registry, say hello to ServiceManager

ZF2 Directory Structure

Module Dir Structure

MVC Process

Event Driven Architecture

public/index.php

Bootstrapping under the hoodZend\Mvc\Application::init()->run()

Running the MVC Application

\\index.php

Zend\Mvc\Application::init()->run()

Service Manager

...

What is a Service Manager?● It is simply a Registry (like Zend_Registry) but it can do much more!

● It provides a clean and simple way to configure complex objects with many dependencies.

● Allows the developer to use IoC or Inversion of control = Dependency Injection.

● Will "prevent" dependencies therefore easier for classes to be unit tested.

ServiceManager Features● Invokables*● Factories*● Aliases● Initializers*● Services*● Shared

ServiceManager - ServiceHow to Register Services to the ServiceManager

Direct:$serviceManager->setService('some_service', new SomeService());$someService = $serviceManager->get('some_service');

In module configuration:array(

'services' => array('some_service' => new SomeService(),

));

ServiceManager - InvokablesHow to register Invokables to the ServiceManager

Direct:$serviceManager->setInvokableClass('MyIndexController', 'MyModule\Controller\IndexController');$indexController = $serviceManager->get('MyIndexController');

In module configuration:array(

'invokables' => array('MyIndexController' => 'MyModule\Controller\IndexController'

));

ServiceManager - FactoriesHow to register factories to the ServiceManager

Direct:$serviceManager->setFactory('user_mapper', function($serviceManager) { $mapper = new \MyModule\Mapper\User; $dbAdapter = $serviceManager->get('db_adapter'); $mapper->setDbAdapter($dbAdapter); return $mapper;});$userMapper = $serviceManager->get('user_mapper');

In module configuration:array(

'factories' => array('user_mapper' => function ($srerviceManager) {

$mapper = new \MyModule\Mapper\User;$dbAdapter = $serviceManager->get('db_adapter');$mapper->setDbAdapter($dbAdapter);return $mapper;

})

)

ServiceManager - Initializers// SomeServiceInterface.phpinterface LocServiceAwareInterface{

public function setLocService(LocService $locService);}

Usage:class SomeClass implements LocServiceAwareInterface{

public $locService;public function setLocService(LocService $LocService){

$this->locService = $LocService;}

}

// register SomeClass to the ServiceManager, you can do this in the module config:$serviceManager->setInvokables('some_class', 'Module\To\SomeClass');

// pretend we are inside an Action in some Controller$someClass = $this->getServiceLocator()->get('some_class');var_dump($someClass->locService); // SomeClass will have an instance of LocService automatically injected

Initializers... ContinueHow to register Initializers to the ServiceManager

Direct:$serviceManager->addInitializer(function ($instance, $serviceLocator) {

if ($instance instanceof LocServiceAwareInterface) {$locService = new LocService();

$instance->setSomeService($locService); }});

Configuration:array(

'initializers' => array(function ($instance, $serviceLocator) {

if ($instance instanceof LocServiceAwareInterface) {$locService = new LocService();

$instance->setLocService($locService); }}

));

EventManager

...

EventManager - Terminology● EventManager - is just an object “manager”

that holds collection of listeners for one or more named events and which trigger events.

● Event - an action● Listener - is a php callback that can react to

an event● Target - is an object that creates events

EventManager exampleuse Zend\EventManager;class LoadClass implements EventManager\EventManagerAwareInterface{

protected $em;public function loader(){

$this->em->trigger('loading', $this, array('I am trying to load yo!'));}

public function setEventManager(EventManager\EventManagerInterface $eventManager){

$this->em = $eventManager;}

}

// pretend we are inside some action in some controller$loadClass = $this->getServiceLocator()->get('LoadClass');$loadClass->getEventManager()->attach('loading', function (EventManager\Event $event) {

echo "loading something...";var_dump(get_class($event->getTarget));var_dump($event->getParams());

});$loadClass->loader();

// loader() will output...."loading something..."LoadClassarray("I am trying to load yo!")

Listener callback

Event name

Target

Coding time...