Top Banner
Zend Framework 2 An introduction
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Zend Framework 2 Intro

Zend Framework 2An introduction

Page 2: Zend Framework 2 Intro

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

Page 3: Zend Framework 2 Intro

MVC Overview

...

Page 4: Zend Framework 2 Intro

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

Page 5: Zend Framework 2 Intro

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

Page 6: Zend Framework 2 Intro

ZF2 Directory Structure

Page 7: Zend Framework 2 Intro

Module Dir Structure

Page 8: Zend Framework 2 Intro

MVC Process

Page 9: Zend Framework 2 Intro

Event Driven Architecture

Page 10: Zend Framework 2 Intro

public/index.php

Page 11: Zend Framework 2 Intro

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

Page 12: Zend Framework 2 Intro

Running the MVC Application

\\index.php

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

Page 13: Zend Framework 2 Intro

Service Manager

...

Page 14: Zend Framework 2 Intro

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.

Page 15: Zend Framework 2 Intro

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

Page 16: Zend Framework 2 Intro

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(),

));

Page 17: Zend Framework 2 Intro

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'

));

Page 18: Zend Framework 2 Intro

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;

})

)

Page 19: Zend Framework 2 Intro

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

Page 20: Zend Framework 2 Intro

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); }}

));

Page 21: Zend Framework 2 Intro

EventManager

...

Page 22: Zend Framework 2 Intro

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

Page 23: Zend Framework 2 Intro

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

Page 24: Zend Framework 2 Intro

Coding time...