SlideShare a Scribd company logo
Zend Framework 2
Basic components
Who is this guy?
name:
     Mateusz Tymek
age:
     26
job:
     Developer at
Zend Framework 2

Zend Framework 1 is great! Why do we need
new version?
Zend Framework 2

Zend Framework 1 is great! Why do we need
new version?

●   ZF1 is inflexible
●   performance sucks
●   difficult to learn
●   doesn't use PHP 5.3 goodies
Zend Framework 2
● development started in 2010
● latest release is BETA3
● release cycle is following the "Gmail" style of
  betas
● developed on GitHub
● no CLA needed anymore!
● aims to provide modern, fast web
  framework...
● ...that solves all problems with its
  predecessor
ZF1             ZF2
application     config
  configs       module
  controllers     Application
  modules            config
  views              src
library                Application
public                    Controller
                     view
                public
                vendor
New module system
module
  Application
     config            "A Module is a
     public            collection of code and
     src
       Application
                       other files that solves
          Controller
                       a more specific
          Form         atomic problem of the
          Model        larger business
          Service      problem"
     view
Module class
class Module implements AutoloaderProvider {
        public function init(Manager $moduleManager)   // module initialization
        {}


        public function getAutoloaderConfig()   // configure PSR-0 autoloader
        {
            return array(
               'ZendLoaderStandardAutoloader' => array(
                    'namespaces' => array(
                        'Application' => __DIR__ . '/src/Application ',
                    )));
        }


        public function getConfig() // provide module configuration
        {
            return include __DIR__ . '/config/module.config.php';
        }
    }
Module configuration

Default:                                          User override:

modules/Doctrine/config/module.config.php         config/autoload/doctrine.local.config.php

return array(                                     return array(
   // connection parameters                           // connection parameters
   'connection' => array(                             'connection' => array(
       'driver'   => 'pdo_mysql',                         'host'     => 'localhost',
       'host'     => 'localhost',                         'user'     => 'username',
       'port'     => '3306',                              'password' => 'password',
       'user'     => 'username',                          'dbname'   => 'database',
       'password' => 'password',                      ),
       'dbname'   => 'database',                  );
   ),
   // driver settings
   'driver' => array(
       'class'     =>
'DoctrineORMMappingDriverAnnotationDriver',
       'namespace' => 'ApplicationEntity',
      ),
);
ZendEventManager
Why do we need aspect-oriented
programming?
ZendEventManager

Define your events

ZendModuleManager    ZendMvcApplication

● loadModules.pre      ●   bootstrap
● loadModule.resolve   ●   route
● loadModules.post     ●   dispatch
                       ●   render
                       ●   finish
ZendEventManager

Attach event listeners
class Module implements AutoloaderProvider
{
    public function init(Manager $moduleManager)
    {
        $events       = $moduleManager->events();
        $sharedEvents = $events->getSharedManager();

        $sharedEvents->attach(
              'ZendMvcApplication', 'finish',
               function(Event $e) {
                    echo memory_get_usage();
               });
    }
}
Example: blog engine
class BlogEngine {
    public $events;

    public $blogPost;

    public function __construct() {
        $this->events = new EventManager(__CLASS__);
    }

    public function showBlogPost() {
        $this->events->trigger('render', $this);
        echo $this->blogPost;
    }
}
Example: blog engine
class BlogEngine {
    public $events;

    public $blogPost;

    public function __construct() {
        $this->events = new EventManager(__CLASS__);
    }

    public function showBlogPost() {
        $this->events->trigger('render', $this);
        echo $this->blogPost;
    }
}

// ...
$blogEngine->events->attach('render', function($event) {
    $engine = $event->getTarget();
    $engine->blogPost = strip_tags($engine->blogPost);
});
Dependency Injection

How do you manage your dependencies?
● globals, singletons, registry
public function indexAction() {
   global $application;
   $user = Zend_Auth::getInstance()->getIdentity();
   $db = Zend_Registry::get('db');
}
Dependency Injection

How do you manage your dependencies?
● Zend_Application_Bootstrap
public function _initPdo() {
   $pdo = new PDO(...);
   return $pdo;
}

public function _initTranslations() {
   $this->bootstrap('pdo');
   $pdo = $this->getResource('pdo'); // dependency!
   $stmt = $pdo->prepare('SELECT * FROM translations');
   // ...
}
Solution: ZendDi

● First, let's consider simple service class:
class UserService {
   protected $pdo;

    public function __construct($pdo) {
       $this->pdo = $pdo;
    }

    public function fetchAll() {
       $stmt = $this->pdo->prepare('SELECT * FROM users');
       $stmt->execute();
       return $stmt->fetchAll();
    }
}
Solution: ZendDi

● Wire it with PDO, using DI configuration:
return array(
  'di' => array(
      'instance' => array(
         'PDO' => array(
            'parameters' => array(
                 'dsn' => 'mysql:dbname=test;host=127.0.0.1',
                 'username' => 'root',
                 'passwd' => ''
          )),

         'UserService' => array(
            'parameters' => array(
                'pdo' => 'PDO'
         )
)));
Solution: ZendDi

● Use it from controllers:
public function indexAction() {

    $sUsers = $this->locator->get('UserService');

    $listOfUsers = $sUsers->fetchAll();

}
Definitions can be complex.
return array(
   'di' => array(
       'instance' => array(
           'ZendViewRendererPhpRenderer' => array(
                'parameters' => array(
                     'resolver' => 'ZendViewResolverAggregateResolver',
                ),
           ),
           'ZendViewResolverAggregateResolver' => array(
                'injections' => array(
                     'ZendViewResolverTemplateMapResolver',
                     'ZendViewResolverTemplatePathStack',
                ),
           ),
           // Defining where the layout/layout view should be located
           'ZendViewResolverTemplateMapResolver' => array(
                'parameters' => array(
                     'map'   => array(
                          'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
                     ),
                ),
           // ...




                                                                 This could go on and on...
Solution: ZendServiceLocator

Simplified application config:
return array(
    'view_manager' => array(
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_path_stack' => array(
            'application' => __DIR__ . '/../view',
        ),
    ),
);
What about performance?

 ●   PSR-0 loader

 ●   cache where possible

 ●   DiC

 ●   accelerator modules:
     EdpSuperluminal, ZfModuleLazyLoading
More info

● http://zendframework.com/zf2/

● http://zend-framework-community.634137.n4.
  nabble.com/

● https://github.com/zendframework/zf2

● http://modules.zendframework.com/

● http://mwop.net/blog.html
Thank you!

More Related Content

PPT
Zend Framework 2
PDF
A quick start on Zend Framework 2
PDF
Zend Framework 2 quick start
PDF
Quick start on Zend Framework 2
PDF
Deprecated: Foundations of Zend Framework 2
PDF
ZF2 Modular Architecture - Taking advantage of it
PDF
Zend Framework 2 Components
PPT
Zend Framework Introduction
Zend Framework 2
A quick start on Zend Framework 2
Zend Framework 2 quick start
Quick start on Zend Framework 2
Deprecated: Foundations of Zend Framework 2
ZF2 Modular Architecture - Taking advantage of it
Zend Framework 2 Components
Zend Framework Introduction

What's hot (20)

PDF
Instant ACLs with Zend Framework 2
PDF
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
PPTX
Get Started with Zend Framework 2
PPTX
Zf2 phpquebec
PDF
Manage cloud infrastructures using Zend Framework 2 (and ZF1)
PDF
ZFConf 2012: Capistrano для деплоймента PHP-приложений (Роман Лапин)
PPT
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
PDF
Zend Framework 2 Patterns
PDF
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
PDF
Cryptography with Zend Framework
PDF
You must know about CodeIgniter Popular Library
PDF
Modular architecture today
PDF
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
PDF
Introduction to Zend framework
ODP
Introduction to Zend Framework
PDF
How to build customizable multitenant web applications - PHPBNL11
PPT
2007 Zend Con Mvc Edited Irmantas
PDF
Apigility reloaded
KEY
Extending Zend_Tool
PDF
ZF2 Presentation @PHP Tour 2011 in Lille
Instant ACLs with Zend Framework 2
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
Get Started with Zend Framework 2
Zf2 phpquebec
Manage cloud infrastructures using Zend Framework 2 (and ZF1)
ZFConf 2012: Capistrano для деплоймента PHP-приложений (Роман Лапин)
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
Zend Framework 2 Patterns
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
Cryptography with Zend Framework
You must know about CodeIgniter Popular Library
Modular architecture today
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
Introduction to Zend framework
Introduction to Zend Framework
How to build customizable multitenant web applications - PHPBNL11
2007 Zend Con Mvc Edited Irmantas
Apigility reloaded
Extending Zend_Tool
ZF2 Presentation @PHP Tour 2011 in Lille
Ad

Viewers also liked (14)

PPTX
A SOA approximation on symfony
PPT
PHP Frameworks and CodeIgniter
PPTX
Presentation1
PDF
PHP is the King, nodejs the prince and python the fool
PDF
Zend Framework 2 - Best Practices
PPTX
A brief overview of java frameworks
PDF
Zend Framework 2 : Dependency Injection
PPT
Php Frameworks
ODP
CodeIgniter PHP MVC Framework
PPTX
Work at GlobalLogic India
PDF
RESTful API Design & Implementation with CodeIgniter PHP Framework
PPT
Why MVC?
PDF
Model View Controller (MVC)
PPT
JavaScript Frameworks and Java EE – A Great Match
A SOA approximation on symfony
PHP Frameworks and CodeIgniter
Presentation1
PHP is the King, nodejs the prince and python the fool
Zend Framework 2 - Best Practices
A brief overview of java frameworks
Zend Framework 2 : Dependency Injection
Php Frameworks
CodeIgniter PHP MVC Framework
Work at GlobalLogic India
RESTful API Design & Implementation with CodeIgniter PHP Framework
Why MVC?
Model View Controller (MVC)
JavaScript Frameworks and Java EE – A Great Match
Ad

Similar to Zend Framework 2 - Basic Components (20)

PDF
ZF2 for the ZF1 Developer
PDF
Symfony2 from the Trenches
PDF
Symfony2 - from the trenches
PPT
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
KEY
Zend Framework Study@Tokyo #2
PPTX
Drupal 8 migrate!
PDF
"Angular.js Concepts in Depth" by Aleksandar Simović
PDF
Doctrine with Symfony - SymfonyCon 2019
PDF
Lviv 2013 d7 vs d8
PDF
関西PHP勉強会 php5.4つまみぐい
PPTX
Beyond DOMReady: Ultra High-Performance Javascript
PDF
What's New In Laravel 5
PDF
Andy Postnikov - Drupal 7 vs Drupal 8: от бутстрапа до рендера
PDF
Lviv 2013 d7 vs d8
PDF
How Kris Writes Symfony Apps
PPTX
Magento Dependency Injection
PPTX
AngularJS Internal
PPTX
AngularJS Architecture
PPTX
Getting up and running with Zend Framework
ZF2 for the ZF1 Developer
Symfony2 from the Trenches
Symfony2 - from the trenches
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework Study@Tokyo #2
Drupal 8 migrate!
"Angular.js Concepts in Depth" by Aleksandar Simović
Doctrine with Symfony - SymfonyCon 2019
Lviv 2013 d7 vs d8
関西PHP勉強会 php5.4つまみぐい
Beyond DOMReady: Ultra High-Performance Javascript
What's New In Laravel 5
Andy Postnikov - Drupal 7 vs Drupal 8: от бутстрапа до рендера
Lviv 2013 d7 vs d8
How Kris Writes Symfony Apps
Magento Dependency Injection
AngularJS Internal
AngularJS Architecture
Getting up and running with Zend Framework

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
NewMind AI Weekly Chronicles - August'25 Week I
sap open course for s4hana steps from ECC to s4
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
Understanding_Digital_Forensics_Presentation.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding

Zend Framework 2 - Basic Components

  • 2. Who is this guy? name: Mateusz Tymek age: 26 job: Developer at
  • 3. Zend Framework 2 Zend Framework 1 is great! Why do we need new version?
  • 4. Zend Framework 2 Zend Framework 1 is great! Why do we need new version? ● ZF1 is inflexible ● performance sucks ● difficult to learn ● doesn't use PHP 5.3 goodies
  • 5. Zend Framework 2 ● development started in 2010 ● latest release is BETA3 ● release cycle is following the "Gmail" style of betas ● developed on GitHub ● no CLA needed anymore! ● aims to provide modern, fast web framework... ● ...that solves all problems with its predecessor
  • 6. ZF1 ZF2 application config configs module controllers Application modules config views src library Application public Controller view public vendor
  • 7. New module system module Application config "A Module is a public collection of code and src Application other files that solves Controller a more specific Form atomic problem of the Model larger business Service problem" view
  • 8. Module class class Module implements AutoloaderProvider { public function init(Manager $moduleManager) // module initialization {} public function getAutoloaderConfig() // configure PSR-0 autoloader { return array( 'ZendLoaderStandardAutoloader' => array( 'namespaces' => array( 'Application' => __DIR__ . '/src/Application ', ))); } public function getConfig() // provide module configuration { return include __DIR__ . '/config/module.config.php'; } }
  • 9. Module configuration Default: User override: modules/Doctrine/config/module.config.php config/autoload/doctrine.local.config.php return array( return array( // connection parameters // connection parameters 'connection' => array( 'connection' => array( 'driver' => 'pdo_mysql', 'host' => 'localhost', 'host' => 'localhost', 'user' => 'username', 'port' => '3306', 'password' => 'password', 'user' => 'username', 'dbname' => 'database', 'password' => 'password', ), 'dbname' => 'database', ); ), // driver settings 'driver' => array( 'class' => 'DoctrineORMMappingDriverAnnotationDriver', 'namespace' => 'ApplicationEntity', ), );
  • 10. ZendEventManager Why do we need aspect-oriented programming?
  • 11. ZendEventManager Define your events ZendModuleManager ZendMvcApplication ● loadModules.pre ● bootstrap ● loadModule.resolve ● route ● loadModules.post ● dispatch ● render ● finish
  • 12. ZendEventManager Attach event listeners class Module implements AutoloaderProvider { public function init(Manager $moduleManager) { $events = $moduleManager->events(); $sharedEvents = $events->getSharedManager(); $sharedEvents->attach( 'ZendMvcApplication', 'finish', function(Event $e) { echo memory_get_usage(); }); } }
  • 13. Example: blog engine class BlogEngine { public $events; public $blogPost; public function __construct() { $this->events = new EventManager(__CLASS__); } public function showBlogPost() { $this->events->trigger('render', $this); echo $this->blogPost; } }
  • 14. Example: blog engine class BlogEngine { public $events; public $blogPost; public function __construct() { $this->events = new EventManager(__CLASS__); } public function showBlogPost() { $this->events->trigger('render', $this); echo $this->blogPost; } } // ... $blogEngine->events->attach('render', function($event) { $engine = $event->getTarget(); $engine->blogPost = strip_tags($engine->blogPost); });
  • 15. Dependency Injection How do you manage your dependencies? ● globals, singletons, registry public function indexAction() { global $application; $user = Zend_Auth::getInstance()->getIdentity(); $db = Zend_Registry::get('db'); }
  • 16. Dependency Injection How do you manage your dependencies? ● Zend_Application_Bootstrap public function _initPdo() { $pdo = new PDO(...); return $pdo; } public function _initTranslations() { $this->bootstrap('pdo'); $pdo = $this->getResource('pdo'); // dependency! $stmt = $pdo->prepare('SELECT * FROM translations'); // ... }
  • 17. Solution: ZendDi ● First, let's consider simple service class: class UserService { protected $pdo; public function __construct($pdo) { $this->pdo = $pdo; } public function fetchAll() { $stmt = $this->pdo->prepare('SELECT * FROM users'); $stmt->execute(); return $stmt->fetchAll(); } }
  • 18. Solution: ZendDi ● Wire it with PDO, using DI configuration: return array( 'di' => array( 'instance' => array( 'PDO' => array( 'parameters' => array( 'dsn' => 'mysql:dbname=test;host=127.0.0.1', 'username' => 'root', 'passwd' => '' )), 'UserService' => array( 'parameters' => array( 'pdo' => 'PDO' ) )));
  • 19. Solution: ZendDi ● Use it from controllers: public function indexAction() { $sUsers = $this->locator->get('UserService'); $listOfUsers = $sUsers->fetchAll(); }
  • 20. Definitions can be complex. return array( 'di' => array( 'instance' => array( 'ZendViewRendererPhpRenderer' => array( 'parameters' => array( 'resolver' => 'ZendViewResolverAggregateResolver', ), ), 'ZendViewResolverAggregateResolver' => array( 'injections' => array( 'ZendViewResolverTemplateMapResolver', 'ZendViewResolverTemplatePathStack', ), ), // Defining where the layout/layout view should be located 'ZendViewResolverTemplateMapResolver' => array( 'parameters' => array( 'map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', ), ), // ... This could go on and on...
  • 21. Solution: ZendServiceLocator Simplified application config: return array( 'view_manager' => array( 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_path_stack' => array( 'application' => __DIR__ . '/../view', ), ), );
  • 22. What about performance? ● PSR-0 loader ● cache where possible ● DiC ● accelerator modules: EdpSuperluminal, ZfModuleLazyLoading
  • 23. More info ● http://zendframework.com/zf2/ ● http://zend-framework-community.634137.n4. nabble.com/ ● https://github.com/zendframework/zf2 ● http://modules.zendframework.com/ ● http://mwop.net/blog.html