A quick start on
Zend Framework 2
by Enrico Zimuel (enrico@zend.com)

Senior Software Engineer
Zend Framework Core Team
Zend Technologies Ltd




         19th May 2012 Verona (Italy)
                                        © All rights reserved. Zend Technologies, Inc.
About me

               • Enrico Zimuel (@ezimuel)
               • Software Engineer since 1996
                      – Assembly x86, C/C++, Java, Perl, PHP
               • PHP Engineer at Zend in the Zend
                            Framework Core Team
               • International speaker about PHP and
                            computer security topics
               • Co-author of the italian book
                            “PHP Best practices” (FAG edizioni)
               • Co-founder of the PUG Torino




           © All rights reserved. Zend Technologies, Inc.
ZF2 in a slide

 ●
     New architecture (MVC, Di, Events)
 ●   Requirement: PHP 5.3.3
 ●   No more CLA (Contributor License Agreement)
 ●   Git (GitHub) instead of SVN
 ●
     Better performance (new autoload)
 ●
     Module support
 ●
     Packaging system (pyrus)



                      © All rights reserved. Zend Technologies, Inc.
A new core

●
    The ZF1 way:
       ▶   Singleton, Registry, and Hard-
              Coded Dependencies
●   The ZF2 approach:
       ▶   Aspect Oriented Design and
             Dependency Injection




                       © All rights reserved. Zend Technologies, Inc.
New architectural approach

●   Methodologies used in the development
      – Decoupling (ZendDi)
      – Event driven (ZendEventManager)
      – Standard classes (ZendStdlib)
●   Take advantage of PHP 5.3
       ▶   Namespace
       ▶   Lambda Functions and Closures
       ▶   Better performance


                       © All rights reserved. Zend Technologies, Inc.
Releases

●   ZF2.0.0beta4 next week!
●   Goal:
     ▶   beta5 on June
     ▶   ZF 2.0 RC this summer!!!




                         © All rights reserved. Zend Technologies, Inc.
Autoloading




  © All rights reserved. Zend Technologies, Inc.
Autoloading

●   No more require_once calls!
●
    Multiple approaches:
      – ZF1-style include_path autoloader
      – Per-namespace/prefix autoloading
      – Class-map autoloading




                    © All rights reserved. Zend Technologies, Inc.
ZF1-Style


require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new ZendLoaderStandardAutoloader(array(
    'fallback_autoloader' => true,
));

$loader->register();




                    © All rights reserved. Zend Technologies, Inc.
ZF2 NS/Prefix

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new ZendLoaderStandardAutoloader();

$loader->registerNamespace(
           'My', __DIR__ . '/../library/My')
      ->registerPrefix(
           'Foo_', __DIR__ . '/../library/Foo');

$loader->register();




                       © All rights reserved. Zend Technologies, Inc.
ZF2 Class-Map

return array(
    'MyFooBar' => __DIR__ . '/Foo/Bar.php',
);
                                                                        .classmap.php


require_once 'Zend/Loader/ClassMapAutoloader.php';

$loader = new ZendLoaderClassMapAutoloader();

$loader->registerAutoloadMap(
    __DIR__ . '/../library/.classmap.php');

$loader->register();



                       © All rights reserved. Zend Technologies, Inc.
Classmap generator

●   How to generate the .classmap.php?
     We provided a command line tool:
     bin/classmap_generator.php
●
    Usage is trivial:

    $ cd your/library
    $ php /path/to/classmap_generator.php -w

●
    Class-Map will be created in .classmap.php




                        © All rights reserved. Zend Technologies, Inc.
Performance improvement

●
    Compared with the ZF1 autoloader
    ▶   Class-Maps
          show a 25-85% improvement
    ▶   Namespaces/prefixes
          shows 10-40% improvement




Note: The new autoloading system of ZF2 has been ported to ZF 1.12


                            © All rights reserved. Zend Technologies, Inc.
Dependency
 Injection



  © All rights reserved. Zend Technologies, Inc.
ZendDi

 ●   Supports the 3 different injection patterns:
        – Constructor
        – Interface
        – Setter
 ●   Implements a Di Container:
        – Manage the dependencies using configuration and
            annotation
        – Provide a compiler to autodiscover classes in a path
             and create the class definitions, for dependencies



                        © All rights reserved. Zend Technologies, Inc.
Example

 class Bar
 { … }

 class Foo
 {
    protected $bar;
    …
    public function setBar(Bar $bar)
    {
       $this->bar = $bar;
    }
    …
 }


                © All rights reserved. Zend Technologies, Inc.
Sample definition


 $definition = array(
     'Foo' => array(
         'setBar' => array(
             'bar' => array(
                 'type'     => 'Bar',
                 'required' => true,
             ),
         ),
     ),
 );




                 © All rights reserved. Zend Technologies, Inc.
Using the Di container

 use ZendDiDi,
     ZendDiConfiguration;

 $di     = new Di;
 $config = new Configuration(array(
     'definition' => array('class' => $definition)
 ));
 $config->configure($di);

 $foo = $di->get('Foo'); // contains Bar!




                   © All rights reserved. Zend Technologies, Inc.
Di by annotation

 use ZendDiDefinitionAnnotation as Di;

 class Foo {
    protected $bar;
    /**
     * @DiInject()
     */
    public function setBar(Bar $bar){
        $this->bar = $bar;
    }
 }

 class Bar { ... }


                     © All rights reserved. Zend Technologies, Inc.
Di by annotation (2)


$compiler = new ZendDiDefinitionCompilerDefinition();
$compiler->addDirectory('File path of Foo and Bar');
$compiler->compile();

$definitions = new ZendDiDefinitionList($compiler);
$di = new ZendDiDi($definitions);

$baz = $di->get('Foo'); // contains Bar!



More use cases of ZendDi:
     https://github.com/ralphschindler/zf2-di-use-cases


                        © All rights reserved. Zend Technologies, Inc.
Event Manager




   © All rights reserved. Zend Technologies, Inc.
Event Manager

●
    An Event Manager is an object that aggregates
    listeners for one or more named events, and
    which triggers events.
●
    A Listener is a callback that can react to an
    event.
●
    An Event is an action.




                      © All rights reserved. Zend Technologies, Inc.
Example

use ZendEventManagerEventManager;

$events = new EventManager();
$events->attach('do', function($e) {
    $event = $e->getName();
    $params = $e->getParams();
    printf(
        'Handled event “%s”, with parameters %s',
        $event,
        json_encode($params)
    );
});
$params = array('foo' => 'bar', 'baz' => 'bat');
$events->trigger('do', null, $params);




                     © All rights reserved. Zend Technologies, Inc.
MVC



© All rights reserved. Zend Technologies, Inc.
Event driven architecture


●   Everything is an event in the MVC architecture of ZF2




                     © All rights reserved. Zend Technologies, Inc.
Module definition



  “A module is a collection of code and other
    files that solves a more specific atomic
   problem of the larger business problem.”
                                                        (from the ZF2 RFC)




                 © All rights reserved. Zend Technologies, Inc.
Module for ZF2

●
    The basic unit in a ZF2 application
    is a Module
●
    Modules are “Plug and play” technology
●
    Modules are simple:
       ▶   A namespace
       ▶   Containing a single classfile: Module.php




                         © All rights reserved. Zend Technologies, Inc.
Quick start
Zend Skeleton Application




       © All rights reserved. Zend Technologies, Inc.
Zend Skeleton Application

●   A simple, skeleton application using the new MVC
    layer and the module system
●
    Github:
    ▶   git clone --recursive
        git://github.com/zendframework/ZendSkeletonApplication.git
●
    This project makes use of Git submodules
●
    Ready for ZF2.0.0beta4




                            © All rights reserved. Zend Technologies, Inc.
Folder's tree

   config
   data
   module
   public
   vendor




                © All rights reserved. Zend Technologies, Inc.
Config folder

  config
         autoload
     application.config.php
  data
  module
  public
  vendor



                    © All rights reserved. Zend Technologies, Inc.
Data folder

  config
  data
         cache
  module
  public
  vendor




                 © All rights reserved. Zend Technologies, Inc.
Module folder
  module
      Application                              Name of the module
               config
                    module.config.php
               src
                      Application
                             Controller
                                  IndexController.php
               view
                      index
                           index.phtml
            Module.php
            autoload_classmap.php
            autoload_functions.php
            autoload_registers.php

                     © All rights reserved. Zend Technologies, Inc.
Public folder

   public
        images
        js
        css
      .htaccess
      index.php




                  © All rights reserved. Zend Technologies, Inc.
Vendor folder

  config
  data
  module
  public
  vendor
         ZendFramework




                  © All rights reserved. Zend Technologies, Inc.
.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]




                 © All rights reserved. Zend Technologies, Inc.
index.php
chdir(dirname(__DIR__));
require_once (getenv('ZF2_PATH') ?: 'vendor/ZF/library') .
'/Zend/Loader/AutoloaderFactory.php';

use ZendLoaderAutoloaderFactory,
   ZendServiceManagerServiceManager,
   ZendMvcServiceServiceManagerConfiguration;

AutoloaderFactory::factory();
$config = include 'config/application.config.php';

$serviceManager = new ServiceManager(new ServiceManagerConfiguration(
$config['service_manager']));
$serviceManager->setService('ApplicationConfiguration', $config);
$serviceManager->get('ModuleManager')->loadModules();

$serviceManager->get('Application')->bootstrap()->run()->send();



                           © All rights reserved. Zend Technologies, Inc.
application.config.php
 return array(
    'modules' => array(
       'Application',
    ),
      'module_listener_options' => array(
         'config_cache_enabled' => false,
         'cache_dir' => 'data/cache',
         'module_paths' => array(
            './module',
            './vendor',
           ),
      ),
      'service_manager' => array(
          'use_defaults' => true,
          'factories' => array(),
      ),
 );
                            © All rights reserved. Zend Technologies, Inc.
Module.php
namespace Application;

class Module
{
   public function getAutoloaderConfig()
   {
      return array(
         'ZendLoaderClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
         ),
         'ZendLoaderStandardAutoloader' => array(
            'namespaces' => array(
               __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
         ),
      );
   }

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

                                   © All rights reserved. Zend Technologies, Inc.
autoload_classmap.php

return array(
     'ApplicationControllerIndexController' =>
          __DIR__ . '/src/Application/Controller/IndexController.php',
     'ApplicationModule' =>
          __DIR__ . '/Module.php',
);




                               © All rights reserved. Zend Technologies, Inc.
module.config.php
return array(
   'router' => array(
      'routes' => array(
         'default' => array(
            'type' => 'ZendMvcRouterHttpSegment',
            'options' => array(
                'route' => '/[:controller[/:action]]',
                'constraints' => array(
                   'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                   'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                   'controller' => 'IndexController',
                   'action' => 'index',
         ),),),
         'home' => array(
            'type' => 'ZendMvcRouterHttpLiteral',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                   'controller' => 'IndexController',
                   'action' => 'index',
          ),),),),),

                                     © All rights reserved. Zend Technologies, Inc.
module.config.php (2)
     'controller' => array(
        'classes' => array(
            'IndexController' => 'ApplicationControllerIndexController'
        ),
     ),
     'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'          => true,
        'doctype'                     => 'HTML5',
        'not_found_template'          => 'error/404',
        'exception_template'          => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'index/index' => __DIR__ . '/../view/index/index.phtml',
            'error/404'     => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            'application' => __DIR__ . '/../view',
        ),
     ),
);


                                      © All rights reserved. Zend Technologies, Inc.
IndexController.php

namespace ApplicationController;

use ZendMvcControllerActionController,
    ZendViewModelViewModel;

class IndexController extends ActionController
{
   public function indexAction()
   {
      return new ViewModel();
   }
}




                         © All rights reserved. Zend Technologies, Inc.
Packaging system




    © All rights reserved. Zend Technologies, Inc.
Source package

 ●
     http://packages.zendframework.com/
 ●   Download or use pyrus, a PEAR2 installer
 ●
     Pyrus packages:
      ▶   Pyrus setup
      ▶   wget http://packages.zendframework.com/pyrus.phar
      ▶   pyrus.phar .
      ▶   pyrus.phar . channel­discover packages.zendframework.com

      ▶   Install a Zend_<component>
      ▶   pyrus.phar . install zf2/Zend_<component>




                         © All rights reserved. Zend Technologies, Inc.
From ZF1 to ZF2




    © All rights reserved. Zend Technologies, Inc.
Migrate to ZF2

●   Goal: migrate without rewriting much code!
●   Main steps
      – Namespace: Zend_Foo => ZendFoo
      – Exceptions: an Interface for each components,
          no more Zend_Exception
      – Autoloading: 3 options (one is ZF1)
      – MVC: module, event based, dispatchable
      – DB: new ZendDb
      – Form: new ZendForm

                     © All rights reserved. Zend Technologies, Inc.
ZF1 migration prototype

●
    Source code: http://bit.ly/pvc0X1
●
    Creates a "Zf1Compat" version of the ZF1 dispatcher
    as an event listener.
●
    The bootstrap largely mimics how ZF1's
    Zend_Application bootstrap works.
●   The default route utilizes the new ZF2 MVC routing,
    but mimics what ZF1 provided




                     © All rights reserved. Zend Technologies, Inc.
How to contribute




    © All rights reserved. Zend Technologies, Inc.
We want you!

●
    How to contribute:
    ▶   Write code
    ▶   Documentation
    ▶   Testing
    ▶   Feedbacks/comments




         https://github.com/zendframework/zf2

                         © All rights reserved. Zend Technologies, Inc.
Helping out

●
    http://framework.zend.com/zf2
●
    http://github.com/zendframework
●   https://github.com/zendframework/ZendSkeletonApplication
●   Getting Started with Zend Framework 2
    by Rob Allen, http://www.akrabat.com
●
    Weekly IRC meetings (#zf2-meeting on Freenode)
●
    #zftalk.2 on Freenode IRC




                        © All rights reserved. Zend Technologies, Inc.
Thank you!

 ●
     Vote this talk:
        ▶   https://joind.in/6384
 ●
     Comments and feedbacks:
        ▶   enrico@zend.com




                       © All rights reserved. Zend Technologies, Inc.

A quick start on Zend Framework 2

  • 1.
    A quick starton Zend Framework 2 by Enrico Zimuel (enrico@zend.com) Senior Software Engineer Zend Framework Core Team Zend Technologies Ltd 19th May 2012 Verona (Italy) © All rights reserved. Zend Technologies, Inc.
  • 2.
    About me • Enrico Zimuel (@ezimuel) • Software Engineer since 1996 – Assembly x86, C/C++, Java, Perl, PHP • PHP Engineer at Zend in the Zend Framework Core Team • International speaker about PHP and computer security topics • Co-author of the italian book “PHP Best practices” (FAG edizioni) • Co-founder of the PUG Torino © All rights reserved. Zend Technologies, Inc.
  • 3.
    ZF2 in aslide ● New architecture (MVC, Di, Events) ● Requirement: PHP 5.3.3 ● No more CLA (Contributor License Agreement) ● Git (GitHub) instead of SVN ● Better performance (new autoload) ● Module support ● Packaging system (pyrus) © All rights reserved. Zend Technologies, Inc.
  • 4.
    A new core ● The ZF1 way: ▶ Singleton, Registry, and Hard- Coded Dependencies ● The ZF2 approach: ▶ Aspect Oriented Design and Dependency Injection © All rights reserved. Zend Technologies, Inc.
  • 5.
    New architectural approach ● Methodologies used in the development – Decoupling (ZendDi) – Event driven (ZendEventManager) – Standard classes (ZendStdlib) ● Take advantage of PHP 5.3 ▶ Namespace ▶ Lambda Functions and Closures ▶ Better performance © All rights reserved. Zend Technologies, Inc.
  • 6.
    Releases ● ZF2.0.0beta4 next week! ● Goal: ▶ beta5 on June ▶ ZF 2.0 RC this summer!!! © All rights reserved. Zend Technologies, Inc.
  • 7.
    Autoloading ©All rights reserved. Zend Technologies, Inc.
  • 8.
    Autoloading ● No more require_once calls! ● Multiple approaches: – ZF1-style include_path autoloader – Per-namespace/prefix autoloading – Class-map autoloading © All rights reserved. Zend Technologies, Inc.
  • 9.
    ZF1-Style require_once 'Zend/Loader/StandardAutoloader.php'; $loader =new ZendLoaderStandardAutoloader(array( 'fallback_autoloader' => true, )); $loader->register(); © All rights reserved. Zend Technologies, Inc.
  • 10.
    ZF2 NS/Prefix require_once 'Zend/Loader/StandardAutoloader.php'; $loader= new ZendLoaderStandardAutoloader(); $loader->registerNamespace( 'My', __DIR__ . '/../library/My') ->registerPrefix( 'Foo_', __DIR__ . '/../library/Foo'); $loader->register(); © All rights reserved. Zend Technologies, Inc.
  • 11.
    ZF2 Class-Map return array( 'MyFooBar' => __DIR__ . '/Foo/Bar.php', ); .classmap.php require_once 'Zend/Loader/ClassMapAutoloader.php'; $loader = new ZendLoaderClassMapAutoloader(); $loader->registerAutoloadMap( __DIR__ . '/../library/.classmap.php'); $loader->register(); © All rights reserved. Zend Technologies, Inc.
  • 12.
    Classmap generator ● How to generate the .classmap.php? We provided a command line tool: bin/classmap_generator.php ● Usage is trivial: $ cd your/library $ php /path/to/classmap_generator.php -w ● Class-Map will be created in .classmap.php © All rights reserved. Zend Technologies, Inc.
  • 13.
    Performance improvement ● Compared with the ZF1 autoloader ▶ Class-Maps show a 25-85% improvement ▶ Namespaces/prefixes shows 10-40% improvement Note: The new autoloading system of ZF2 has been ported to ZF 1.12 © All rights reserved. Zend Technologies, Inc.
  • 14.
    Dependency Injection © All rights reserved. Zend Technologies, Inc.
  • 15.
    ZendDi ● Supports the 3 different injection patterns: – Constructor – Interface – Setter ● Implements a Di Container: – Manage the dependencies using configuration and annotation – Provide a compiler to autodiscover classes in a path and create the class definitions, for dependencies © All rights reserved. Zend Technologies, Inc.
  • 16.
    Example class Bar { … } class Foo { protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } … } © All rights reserved. Zend Technologies, Inc.
  • 17.
    Sample definition $definition= array( 'Foo' => array( 'setBar' => array( 'bar' => array( 'type' => 'Bar', 'required' => true, ), ), ), ); © All rights reserved. Zend Technologies, Inc.
  • 18.
    Using the Dicontainer use ZendDiDi, ZendDiConfiguration; $di = new Di; $config = new Configuration(array( 'definition' => array('class' => $definition) )); $config->configure($di); $foo = $di->get('Foo'); // contains Bar! © All rights reserved. Zend Technologies, Inc.
  • 19.
    Di by annotation use ZendDiDefinitionAnnotation as Di; class Foo { protected $bar; /** * @DiInject() */ public function setBar(Bar $bar){ $this->bar = $bar; } } class Bar { ... } © All rights reserved. Zend Technologies, Inc.
  • 20.
    Di by annotation(2) $compiler = new ZendDiDefinitionCompilerDefinition(); $compiler->addDirectory('File path of Foo and Bar'); $compiler->compile(); $definitions = new ZendDiDefinitionList($compiler); $di = new ZendDiDi($definitions); $baz = $di->get('Foo'); // contains Bar! More use cases of ZendDi: https://github.com/ralphschindler/zf2-di-use-cases © All rights reserved. Zend Technologies, Inc.
  • 21.
    Event Manager © All rights reserved. Zend Technologies, Inc.
  • 22.
    Event Manager ● An Event Manager is an object that aggregates listeners for one or more named events, and which triggers events. ● A Listener is a callback that can react to an event. ● An Event is an action. © All rights reserved. Zend Technologies, Inc.
  • 23.
    Example use ZendEventManagerEventManager; $events =new EventManager(); $events->attach('do', function($e) { $event = $e->getName(); $params = $e->getParams(); printf( 'Handled event “%s”, with parameters %s', $event, json_encode($params) ); }); $params = array('foo' => 'bar', 'baz' => 'bat'); $events->trigger('do', null, $params); © All rights reserved. Zend Technologies, Inc.
  • 24.
    MVC © All rightsreserved. Zend Technologies, Inc.
  • 25.
    Event driven architecture ● Everything is an event in the MVC architecture of ZF2 © All rights reserved. Zend Technologies, Inc.
  • 26.
    Module definition “A module is a collection of code and other files that solves a more specific atomic problem of the larger business problem.” (from the ZF2 RFC) © All rights reserved. Zend Technologies, Inc.
  • 27.
    Module for ZF2 ● The basic unit in a ZF2 application is a Module ● Modules are “Plug and play” technology ● Modules are simple: ▶ A namespace ▶ Containing a single classfile: Module.php © All rights reserved. Zend Technologies, Inc.
  • 28.
    Quick start Zend SkeletonApplication © All rights reserved. Zend Technologies, Inc.
  • 29.
    Zend Skeleton Application ● A simple, skeleton application using the new MVC layer and the module system ● Github: ▶ git clone --recursive git://github.com/zendframework/ZendSkeletonApplication.git ● This project makes use of Git submodules ● Ready for ZF2.0.0beta4 © All rights reserved. Zend Technologies, Inc.
  • 30.
    Folder's tree config data module public vendor © All rights reserved. Zend Technologies, Inc.
  • 31.
    Config folder config autoload application.config.php data module public vendor © All rights reserved. Zend Technologies, Inc.
  • 32.
    Data folder config data cache module public vendor © All rights reserved. Zend Technologies, Inc.
  • 33.
    Module folder module Application Name of the module config module.config.php src Application Controller IndexController.php view index index.phtml Module.php autoload_classmap.php autoload_functions.php autoload_registers.php © All rights reserved. Zend Technologies, Inc.
  • 34.
    Public folder public images js css .htaccess index.php © All rights reserved. Zend Technologies, Inc.
  • 35.
    Vendor folder config data module public vendor ZendFramework © All rights reserved. Zend Technologies, Inc.
  • 36.
    .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME}-s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] © All rights reserved. Zend Technologies, Inc.
  • 37.
    index.php chdir(dirname(__DIR__)); require_once (getenv('ZF2_PATH') ?:'vendor/ZF/library') . '/Zend/Loader/AutoloaderFactory.php'; use ZendLoaderAutoloaderFactory, ZendServiceManagerServiceManager, ZendMvcServiceServiceManagerConfiguration; AutoloaderFactory::factory(); $config = include 'config/application.config.php'; $serviceManager = new ServiceManager(new ServiceManagerConfiguration( $config['service_manager'])); $serviceManager->setService('ApplicationConfiguration', $config); $serviceManager->get('ModuleManager')->loadModules(); $serviceManager->get('Application')->bootstrap()->run()->send(); © All rights reserved. Zend Technologies, Inc.
  • 38.
    application.config.php return array( 'modules' => array( 'Application', ), 'module_listener_options' => array( 'config_cache_enabled' => false, 'cache_dir' => 'data/cache', 'module_paths' => array( './module', './vendor', ), ), 'service_manager' => array( 'use_defaults' => true, 'factories' => array(), ), ); © All rights reserved. Zend Technologies, Inc.
  • 39.
    Module.php namespace Application; class Module { public function getAutoloaderConfig() { return array( 'ZendLoaderClassMapAutoloader' => array( __DIR__ . '/autoload_classmap.php', ), 'ZendLoaderStandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } } © All rights reserved. Zend Technologies, Inc.
  • 40.
    autoload_classmap.php return array( 'ApplicationControllerIndexController' => __DIR__ . '/src/Application/Controller/IndexController.php', 'ApplicationModule' => __DIR__ . '/Module.php', ); © All rights reserved. Zend Technologies, Inc.
  • 41.
    module.config.php return array( 'router' => array( 'routes' => array( 'default' => array( 'type' => 'ZendMvcRouterHttpSegment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'IndexController', 'action' => 'index', ),),), 'home' => array( 'type' => 'ZendMvcRouterHttpLiteral', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'IndexController', 'action' => 'index', ),),),),), © All rights reserved. Zend Technologies, Inc.
  • 42.
    module.config.php (2) 'controller' => array( 'classes' => array( 'IndexController' => 'ApplicationControllerIndexController' ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'index/index' => __DIR__ . '/../view/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( 'application' => __DIR__ . '/../view', ), ), ); © All rights reserved. Zend Technologies, Inc.
  • 43.
    IndexController.php namespace ApplicationController; use ZendMvcControllerActionController, ZendViewModelViewModel; class IndexController extends ActionController { public function indexAction() { return new ViewModel(); } } © All rights reserved. Zend Technologies, Inc.
  • 44.
    Packaging system © All rights reserved. Zend Technologies, Inc.
  • 45.
    Source package ● http://packages.zendframework.com/ ● Download or use pyrus, a PEAR2 installer ● Pyrus packages: ▶ Pyrus setup ▶ wget http://packages.zendframework.com/pyrus.phar ▶ pyrus.phar . ▶ pyrus.phar . channel­discover packages.zendframework.com ▶ Install a Zend_<component> ▶ pyrus.phar . install zf2/Zend_<component> © All rights reserved. Zend Technologies, Inc.
  • 46.
    From ZF1 toZF2 © All rights reserved. Zend Technologies, Inc.
  • 47.
    Migrate to ZF2 ● Goal: migrate without rewriting much code! ● Main steps – Namespace: Zend_Foo => ZendFoo – Exceptions: an Interface for each components, no more Zend_Exception – Autoloading: 3 options (one is ZF1) – MVC: module, event based, dispatchable – DB: new ZendDb – Form: new ZendForm © All rights reserved. Zend Technologies, Inc.
  • 48.
    ZF1 migration prototype ● Source code: http://bit.ly/pvc0X1 ● Creates a "Zf1Compat" version of the ZF1 dispatcher as an event listener. ● The bootstrap largely mimics how ZF1's Zend_Application bootstrap works. ● The default route utilizes the new ZF2 MVC routing, but mimics what ZF1 provided © All rights reserved. Zend Technologies, Inc.
  • 49.
    How to contribute © All rights reserved. Zend Technologies, Inc.
  • 50.
    We want you! ● How to contribute: ▶ Write code ▶ Documentation ▶ Testing ▶ Feedbacks/comments https://github.com/zendframework/zf2 © All rights reserved. Zend Technologies, Inc.
  • 51.
    Helping out ● http://framework.zend.com/zf2 ● http://github.com/zendframework ● https://github.com/zendframework/ZendSkeletonApplication ● Getting Started with Zend Framework 2 by Rob Allen, http://www.akrabat.com ● Weekly IRC meetings (#zf2-meeting on Freenode) ● #zftalk.2 on Freenode IRC © All rights reserved. Zend Technologies, Inc.
  • 52.
    Thank you! ● Vote this talk: ▶ https://joind.in/6384 ● Comments and feedbacks: ▶ enrico@zend.com © All rights reserved. Zend Technologies, Inc.