SlideShare a Scribd company logo
ZF2 For ZF1 Developers
        Gary Hockin
     5th December 2012
     26th February 2013
Who?
ZF2 for the ZF1 Developer
http://blog.hock.in
          @GeeH
Spabby in #zftalk (freenode)
What?
Why?
THERE WILL BE CODE!
Getting Started
Namespaces
ZF1
class FeaturedController extends Zend_Controller_Action
{
    public function indexAction ()
    {
        $logfile = date('dmY') . '.log';
        $logger = new Zend_Log();
        $writer = new Zend_Log_Writer_Stream ($logfile);
        $logger->addWriter( $writer);
    }
}
ZF2
namespace ApplicationController;

use ZendMvcControllerAbstractActionController;
use ZendLogLogger;
use ZendLogWriterStream as Writer;

class FeaturedController extends AbstractActionController;
{
    public function indexAction ()
    {
        $logfile = date('dmY') . '.log';
        $logger = new Logger();
        $writer = new Writer($logfile);
        $logger->addWriter( $writer);
    }
}
Goodbye...

Long_Underscore_Seperated_Class


           Hello...

 NiceSnappyNamespacedClass
Modules
Bootstrapping
Service Manager
namespace Application;

use ZendMvcControllerAbstractActionController;
use ApplicationServiceMyService;

class MyController extends AbstractActionController
{
    protected $myService;

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

    public function indexAction ()
    {
        return array(
            'myViewVar' => $myService->getMyViewVar()
        );
    }
}
use ApplicationControllerMyController;

public function getServiceConfig ()
{
    return array(
        'factories' => array(
            'myService' => function( ServiceManager $sm) {
                $myService = new ApplicationService MyService();
                $myService->setSomeProperty( true);
                return $myService;
            },
        ),
    );
}

public function getControllerConfig ()
{
    return array(
        'factories' => array(
            'MyController' => function( ServiceLocator $sl) {
                  $myService = $sl->getServiceManager() ->get('myService' );
                  $myController = new MyController ($myService);
                 return $myService;
             },
        ),
    );
}
Dependency Injector
Event Manager
Module.php
public function init(ModuleManager $moduleManager)
{
    $moduleManager->getEventManager() ->attach(
        'loadModules.pre' ,
        function(ZendModuleManager ModuleEvent $e) {
            // do some pre module loading code here
        }
    );
}
Module.php
public function onBootstrap (ModuleManager $moduleManager)
{
    $moduleManager->getEventManager() ->attach(
        'myUser.Logout' ,
        function(ZendModuleManager ModuleEvent $e) {
            // cleanup some cache/temp files here
        }
    );
}

/**
 * MyService class
 * Presume you have passed the EM into $eventManager via SM or Di
 **/
public function logout()
{
      do Logout code
     $this->eventManager ->trigger('myUser.Logout' );
}
Routing
ZF1
;Match route /product/1289 to ProductController DetailsAction with the
parameter ProductId set to 1289

resources.router.routes.product.route = "/product/:ProductId"
resources.router.routes.product.defaults.controller = "product"
resources.router.routes.product.defaults.action = "details"
ZF2
return array(
    'router' => array(
        'routes' => array(
            'category' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/product/:ProductId' ,
                    'constraints' => array(
                        'ProductId' => '[0-9]*'
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'ApplicationController' ,
                        'controller' => 'Product',
                        'action' => 'details',
                    ),
                ),
            ),
            ...
Zend View
Zend View
View Helpers
ZF1
application.ini

 resources.view.helperPath.My_View_Helper   = "My/View/Helper"

My/View/Helper/Bacon.php

 class My_View_Helper_Bacon extends Zend_View_Helper_Abstract
 {
     public function bacon()
     {
         return 'Bacon';
     }
 }
ZF2
Module.php

 public function getViewHelperConfig ()
 {
     return array(
         'invokables' => array(
             'bacon' => 'MyViewHelperBacon'
         )
     );
 }

MyViewHelperBacon.php

 namespace MyViewHelper

 class Bacon extends  ZendViewHelperAbstractHelper
 {
     public function __invoke()
     {
         return 'bacon';
     }
 }
Zend Db
Zend Db Adapter
          &
Zend Db TableGateway
ZF1
application.ini
 resources.db.adapter = "PDO_MYSQL"
 resources.db.isdefaulttableadapter = true
 resources.db.params.dbname = "mydb"
 resources.db.params.username = "root"
 resources.db.params.password = "password"
 resources.db.params.host = "localhost"



ApplicationModelTableUser.php

 class Application_Model_Table_User extends Zend_Db_Table_Abstract
 {
     protected $_name = 'user';

     public function getUser($id)
     {
         return $this->find($id);
     }
 }
ZF2
configautoloaddatabase.local.php
 return array(
     'service_manager' => array(
         'factories' => array(
             'Adapter' => function ( $sm) {
                 return new ZendDbAdapter Adapter(array(
                     'driver'    => 'pdo',
                     'dsn'       => 'mysql:dbname=db;host=127.0.0.1' ,
                     'database' => 'db',
                     'username' => 'user',
                     'password' => 'bacon',
                     'hostname' => '127.0.0.1' ,
                 ));
             },
         ),
     ),
 );
ZF2
ApplicationModule.php
 namespace Application;
 use ZendDbTableGatewayTableGateway;

 class Module
 {
     public function getServiceConfig ()
     {
         return array(
              'factories' => array(
                  'UserTable' => function ( $serviceManager) {
                      $adapter = $serviceManager->get('Adapter');
                      return new TableGateway ('user', $adapter);
                  },
             ),
         );
     }
ZF2
ApplicationControllerIndexController.php
 namespace ApplicationController;

 class IndexController extends AbstractActionController
 {
     public function indexAction ()
     {
         $userTable = $this->getServiceLocator() ->get('UserTable' );
         return new ViewModel(array('user' => $userTable->select(
             array('id' => '12345'))
         ));
     }
 }
Zend Db TableGateway
          &
  Object Hydration
Zend Db TableGateway
          &
  Object Hydration

    http://hock.in/WIP1qa
Zend Form
Zend Form
http://hock.in/11SCIvU
Questions?

     http://blog.hock.in
           @GeeH
 Spabby in #zftalk (freenode)


       Slides and Feedback:

https://joind.in/8247
ZF2 for the ZF1 Developer

More Related Content

PDF
Zend Framework 2 - presentation
PDF
Head First Zend Framework - Part 1 Project & Application
PDF
Alfredo-PUMEX
PPTX
Beyond DOMReady: Ultra High-Performance Javascript
PDF
mapserver_install_linux
PDF
Doctrine 2
PDF
Symfony2 - from the trenches
PPTX
Zend framework
Zend Framework 2 - presentation
Head First Zend Framework - Part 1 Project & Application
Alfredo-PUMEX
Beyond DOMReady: Ultra High-Performance Javascript
mapserver_install_linux
Doctrine 2
Symfony2 - from the trenches
Zend framework

What's hot (20)

PDF
Moodle 3.3 - API Change Overview #mootieuk17
PDF
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PPTX
Zf2 phpquebec
PDF
Introduction to Zend framework
PDF
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
스프링 코어 강의 2부 - Java 구성을 활용한 스프링 코어 사용
PDF
Drupal 8 Services
PPT
An Introduction to Drupal
PPTX
Building Web Apps with Express
PDF
Php unit the-mostunknownparts
PDF
Unit and Functional Testing with Symfony2
ODP
Writing Drupal 5 Module
PPT
Presentation
PPT
Zend - Installation And Sample Project Creation
PDF
The Beauty and the Beast
PDF
PHP Data Objects
PDF
Dependency injection in PHP 5.3/5.4
PDF
Require js and Magento2
PDF
OSGi framework overview
Moodle 3.3 - API Change Overview #mootieuk17
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
Zf2 phpquebec
Introduction to Zend framework
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
스프링 코어 강의 2부 - Java 구성을 활용한 스프링 코어 사용
Drupal 8 Services
An Introduction to Drupal
Building Web Apps with Express
Php unit the-mostunknownparts
Unit and Functional Testing with Symfony2
Writing Drupal 5 Module
Presentation
Zend - Installation And Sample Project Creation
The Beauty and the Beast
PHP Data Objects
Dependency injection in PHP 5.3/5.4
Require js and Magento2
OSGi framework overview
Ad

Similar to ZF2 for the ZF1 Developer (20)

PDF
Zend Framework 2 - Basic Components
PPTX
Adding Dependency Injection to Legacy Applications
PPT
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
PDF
Zend Framework 2 Patterns
DOCX
Zend framework 2.0
PDF
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
PDF
Into the ZF2 Service Manager
PDF
A quick start on Zend Framework 2
PDF
Zend Lab
PDF
symfony on action - WebTech 207
KEY
PDF
Migrating to dependency injection
PDF
ZF2 Modular Architecture - Taking advantage of it
PDF
Quick start on Zend Framework 2
PDF
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
PDF
Zend Framework 2 quick start
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
PDF
Hebrew, Introduction to Zend Controller And new technique
PDF
Models Best Practices (ZF MVC)
KEY
Zendcon 09
Zend Framework 2 - Basic Components
Adding Dependency Injection to Legacy Applications
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
Zend Framework 2 Patterns
Zend framework 2.0
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
Into the ZF2 Service Manager
A quick start on Zend Framework 2
Zend Lab
symfony on action - WebTech 207
Migrating to dependency injection
ZF2 Modular Architecture - Taking advantage of it
Quick start on Zend Framework 2
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
Zend Framework 2 quick start
Zend Framework 1.9 Setup & Using Zend_Tool
Hebrew, Introduction to Zend Controller And new technique
Models Best Practices (ZF MVC)
Zendcon 09
Ad

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
KodekX | Application Modernization Development
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
sap open course for s4hana steps from ECC to s4
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Spectroscopy.pptx food analysis technology
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KodekX | Application Modernization Development
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Programs and apps: productivity, graphics, security and other tools
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
sap open course for s4hana steps from ECC to s4

ZF2 for the ZF1 Developer

  • 1. ZF2 For ZF1 Developers Gary Hockin 5th December 2012 26th February 2013
  • 4. http://blog.hock.in @GeeH Spabby in #zftalk (freenode)
  • 10. ZF1 class FeaturedController extends Zend_Controller_Action { public function indexAction () { $logfile = date('dmY') . '.log'; $logger = new Zend_Log(); $writer = new Zend_Log_Writer_Stream ($logfile); $logger->addWriter( $writer); } }
  • 11. ZF2 namespace ApplicationController; use ZendMvcControllerAbstractActionController; use ZendLogLogger; use ZendLogWriterStream as Writer; class FeaturedController extends AbstractActionController; { public function indexAction () { $logfile = date('dmY') . '.log'; $logger = new Logger(); $writer = new Writer($logfile); $logger->addWriter( $writer); } }
  • 12. Goodbye... Long_Underscore_Seperated_Class Hello... NiceSnappyNamespacedClass
  • 16. namespace Application; use ZendMvcControllerAbstractActionController; use ApplicationServiceMyService; class MyController extends AbstractActionController { protected $myService; public function __construct (MyService $myService) { $this->myService = $myService; } public function indexAction () { return array( 'myViewVar' => $myService->getMyViewVar() ); } }
  • 17. use ApplicationControllerMyController; public function getServiceConfig () { return array( 'factories' => array( 'myService' => function( ServiceManager $sm) { $myService = new ApplicationService MyService(); $myService->setSomeProperty( true); return $myService; }, ), ); } public function getControllerConfig () { return array( 'factories' => array( 'MyController' => function( ServiceLocator $sl) { $myService = $sl->getServiceManager() ->get('myService' ); $myController = new MyController ($myService); return $myService; }, ), ); }
  • 20. Module.php public function init(ModuleManager $moduleManager) { $moduleManager->getEventManager() ->attach( 'loadModules.pre' , function(ZendModuleManager ModuleEvent $e) { // do some pre module loading code here } ); }
  • 21. Module.php public function onBootstrap (ModuleManager $moduleManager) { $moduleManager->getEventManager() ->attach( 'myUser.Logout' , function(ZendModuleManager ModuleEvent $e) { // cleanup some cache/temp files here } ); } /** * MyService class * Presume you have passed the EM into $eventManager via SM or Di **/ public function logout() { do Logout code $this->eventManager ->trigger('myUser.Logout' ); }
  • 23. ZF1 ;Match route /product/1289 to ProductController DetailsAction with the parameter ProductId set to 1289 resources.router.routes.product.route = "/product/:ProductId" resources.router.routes.product.defaults.controller = "product" resources.router.routes.product.defaults.action = "details"
  • 24. ZF2 return array( 'router' => array( 'routes' => array( 'category' => array( 'type' => 'Segment', 'options' => array( 'route' => '/product/:ProductId' , 'constraints' => array( 'ProductId' => '[0-9]*' ), 'defaults' => array( '__NAMESPACE__' => 'ApplicationController' , 'controller' => 'Product', 'action' => 'details', ), ), ), ...
  • 27. ZF1 application.ini resources.view.helperPath.My_View_Helper = "My/View/Helper" My/View/Helper/Bacon.php class My_View_Helper_Bacon extends Zend_View_Helper_Abstract { public function bacon() { return 'Bacon'; } }
  • 28. ZF2 Module.php public function getViewHelperConfig () { return array( 'invokables' => array( 'bacon' => 'MyViewHelperBacon' ) ); } MyViewHelperBacon.php namespace MyViewHelper class Bacon extends ZendViewHelperAbstractHelper { public function __invoke() { return 'bacon'; } }
  • 30. Zend Db Adapter & Zend Db TableGateway
  • 31. ZF1 application.ini resources.db.adapter = "PDO_MYSQL" resources.db.isdefaulttableadapter = true resources.db.params.dbname = "mydb" resources.db.params.username = "root" resources.db.params.password = "password" resources.db.params.host = "localhost" ApplicationModelTableUser.php class Application_Model_Table_User extends Zend_Db_Table_Abstract { protected $_name = 'user'; public function getUser($id) { return $this->find($id); } }
  • 32. ZF2 configautoloaddatabase.local.php return array( 'service_manager' => array( 'factories' => array( 'Adapter' => function ( $sm) { return new ZendDbAdapter Adapter(array( 'driver' => 'pdo', 'dsn' => 'mysql:dbname=db;host=127.0.0.1' , 'database' => 'db', 'username' => 'user', 'password' => 'bacon', 'hostname' => '127.0.0.1' , )); }, ), ), );
  • 33. ZF2 ApplicationModule.php namespace Application; use ZendDbTableGatewayTableGateway; class Module { public function getServiceConfig () { return array( 'factories' => array( 'UserTable' => function ( $serviceManager) { $adapter = $serviceManager->get('Adapter'); return new TableGateway ('user', $adapter); }, ), ); }
  • 34. ZF2 ApplicationControllerIndexController.php namespace ApplicationController; class IndexController extends AbstractActionController { public function indexAction () { $userTable = $this->getServiceLocator() ->get('UserTable' ); return new ViewModel(array('user' => $userTable->select( array('id' => '12345')) )); } }
  • 35. Zend Db TableGateway & Object Hydration
  • 36. Zend Db TableGateway & Object Hydration http://hock.in/WIP1qa
  • 39. Questions? http://blog.hock.in @GeeH Spabby in #zftalk (freenode) Slides and Feedback: https://joind.in/8247