Zend Framework 2quick startby Enrico Zimuel (enrico@zend.com)Senior Software EngineerZend Framework Core TeamZend Technolo...
About me                     • Enrico Zimuel (@ezimuel)                     • Software Engineer since 1996                ...
Summary●   Overview of ZF2●   The new autoloading system●   Dependency Injection●   Event manager●   The new MVC●   Quick ...
ZF2 key features●   New architecture (MVC, Di, Events)●   Requirement: PHP 5.3●   No more CLA (Contributor License Agreeme...
A new core●   The ZF1 way:       ▶   Singleton, Registry, and             Hard-Coded Dependencies●   The ZF2 approach:    ...
New architectural approach●   Methodologies used in the development      – Decoupling (ZendDi)      – Event driven (ZendEv...
Autoloading  © All rights reserved. Zend Technologies, Inc.
Autoloading●   No more require_once calls!●   Multiple approaches:      – ZF1-style include_path autoloader      – Per-nam...
Classmap generator●   How to generate the .classmap.php?     We provided a command line tool:     bin/classmap_generator.p...
Performance improvement●   Class-Maps show a 25% improvement on the      ZF1 autoloader when no acceleration is      prese...
Dependency Injection  © All rights reserved. Zend Technologies, Inc.
Dependency injection●   How to manage dependencies between    objects?●   Dependency injection (Di) is    a design pattern...
Di by construct        Without Di                                                 With Di (construct)class Foo {          ...
Di by setterclass Foo{   protected $bar;   …   public function setBar(Bar $bar)   {      $this->bar = $bar;   }   …}      ...
ZendDi ●   Supports the 3 different injection patterns:       – Constructor       – Interface       – Setter ●   Implement...
Sample definition $definition = array(     Foo => array(         setBar => array(             bar => array(               ...
Using the Di container use ZendDiDi,     ZendDiConfiguration; $di     = new Di; $config = new Configuration(array(     def...
Di by annotationnamespace Example {   use ZendDiDefinitionAnnotation as Di;    class Foo {       public $bam;       /**   ...
Di by annotation (2)$compiler = new ZendDiDefinitionCompilerDefinition();$compiler->addDirectory(File path of Foo and Bar)...
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 tri...
Exampleuse ZendEventManagerEventManager;$events = new EventManager();$events->attach(do, function($e) {    $event = $e->ge...
MVC© All rights reserved. Zend Technologies, Inc.
Event driven architecture●   Flow: bootstrap, route, dispatch, response●   Everything is an event in MVC of ZF2           ...
Modules●   The basic unit in a ZF2 MVC application is a Module●   A module is a collection of code and other files that   ...
Quick startZendSkeletonApplication       © All rights reserved. Zend Technologies, Inc.
ZendSkeletonApplication●   A simple, skeleton application using the ZF2    MVC layer and module systems●   On GitHub:     ...
Folder tree   config                           autoload                          application.config.php   data   module   ...
Output         © All rights reserved. Zend Technologies, Inc.
index.phpchdir(dirname(__DIR__));require_once (getenv(ZF2_PATH) ?:vendor/ZendFramework/library) ./Zend/Loader/AutoloaderFa...
index.php (2)...$moduleManager = new ZendModuleManager($appConfig[modules]);$moduleManager->events()->attachAggregate($def...
config/application.config.phpreturn array(    modules => array(        Application    ),    module_listener_options => arr...
config/autoload●   global.config.php●   local.config.php.dist (.gitignore)●   By default, this application is configured t...
Application/config/module.config.php  return array(      di => array(          instance => array(             …          )...
IndexController.phpnamespace ApplicationController;use ZendMvcControllerActionController,    ZendViewModelViewModel;class ...
Modules are “plug and play”●   Easy to reuse a module, only 3 steps:1) Copy the module in module (or vendor) folder2) Enab...
modules.zendframework.com           © All rights reserved. Zend Technologies, Inc.
Package system    © All rights reserved. Zend Technologies, Inc.
Package system●   We use Pyrus, a tool to manage PEAR    packages. Pyrus simplifies and improves the    PEAR experience.● ...
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      – Ex...
ZF1 migration prototype●   Source code: http://bit.ly/pvc0X1●   Creates a "Zf1Compat" version of the ZF1    dispatcher as ...
Helping out●   http://framework.zend.com/zf2●   http://github.com/zendframework●   https://github.com/zendframework/ZendSk...
Questions?             © All rights reserved. Zend Technologies, Inc.
Thank you!●   Comments and feedbacks:     – Email: enrico@zend.com     – Twitter: @ezimuel                 © All rights re...
Upcoming SlideShare
Loading in...5
×

Zend Framework 2 quick start

17,329

Published on

Zend Framework 2 quick start presentation at ZFCon 2012 of Moscow

Published in: Technology
2 Comments
24 Likes
Statistics
Notes
No Downloads
Views
Total Views
17,329
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
403
Comments
2
Likes
24
Embeds 0
No embeds

No notes for slide

Zend Framework 2 quick start

  1. 1. Zend Framework 2quick startby Enrico Zimuel (enrico@zend.com)Senior Software EngineerZend Framework Core TeamZend Technologies LtdZFConf – 21th April 2012 Moscow © All rights reserved. Zend Technologies, Inc.
  2. 2. About me • Enrico Zimuel (@ezimuel) • Software Engineer since 1996 – Assembly x86, C/C++, Java, Perl, PHP • Enjoying PHP since 1999 • PHP Engineer at Zend since 2008 • Zend Framework Core Team from 2011 • B.Sc. Computer Science and Economics from University of Pescara (Italy) © All rights reserved. Zend Technologies, Inc.
  3. 3. Summary● Overview of ZF2● The new autoloading system● Dependency Injection● Event manager● The new MVC● Quick start: ZendSkeletonApplication● Package system● From ZF1 to ZF2 © All rights reserved. Zend Technologies, Inc.
  4. 4. ZF2 key features● New architecture (MVC, Di, Events)● Requirement: PHP 5.3● No more CLA (Contributor License Agreement)● Git (GitHub) instead of SVN● Better performance● Module management● Packaging system © All rights reserved. Zend Technologies, Inc.
  5. 5. 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.
  6. 6. 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.
  7. 7. Autoloading © All rights reserved. Zend Technologies, Inc.
  8. 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. 9. 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.
  10. 10. Performance improvement● Class-Maps show a 25% improvement on the ZF1 autoloader when no acceleration is present ▶ 60-85% improvements when an opcode cache is in place!● Pairing namespaces/prefixes with specific paths shows >10% gains with no acceleration ▶ 40% improvements when an opcode cache is in place!Note: The new autoloading system of ZF2 has been ported to ZF 1.12 © All rights reserved. Zend Technologies, Inc.
  11. 11. Dependency Injection © All rights reserved. Zend Technologies, Inc.
  12. 12. Dependency injection● How to manage dependencies between objects?● Dependency injection (Di) is a design pattern whose purpose is to reduce the coupling between software components Its time for your dependency injection! © All rights reserved. Zend Technologies, Inc.
  13. 13. Di by construct Without Di With Di (construct)class Foo { class Foo { protected $bar; protected $bar; … … public function __construct() { public function $this->bar= new Bar(); __construct(Bar $bar) { } $this->bar = $bar; … }} … } Cons: Pros: Difficult to test Easy to test No isolation Decoupling Difficult to reuse code Flexible architecture © All rights reserved. Zend Technologies, Inc.
  14. 14. Di by setterclass Foo{ protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } …} © All rights reserved. Zend Technologies, Inc.
  15. 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. 16. Sample definition $definition = array( Foo => array( setBar => array( bar => array( type => Bar, required => true, ), ), ), ); © All rights reserved. Zend Technologies, Inc.
  17. 17. 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.
  18. 18. Di by annotationnamespace Example { use ZendDiDefinitionAnnotation as Di; class Foo { public $bam; /** * @DiInject() */ public function setBar(Bar $bar){ $this->bar = $bar; } } class Bar { }} © All rights reserved. Zend Technologies, Inc.
  19. 19. 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(ExampleFoo); // contains Bar!More use cases of ZendDi:https://github.com/ralphschindler/zf2-di-use-cases © All rights reserved. Zend Technologies, Inc.
  20. 20. Event Manager © All rights reserved. Zend Technologies, Inc.
  21. 21. 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.
  22. 22. Exampleuse 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.
  23. 23. MVC© All rights reserved. Zend Technologies, Inc.
  24. 24. Event driven architecture● Flow: bootstrap, route, dispatch, response● Everything is an event in MVC of ZF2 © All rights reserved. Zend Technologies, Inc.
  25. 25. Modules● The basic unit in a ZF2 MVC application is a Module● A module is a collection of code and other files that solves a more specific atomic problem of the larger business problem● Modules are simply: ▶ A namespace ▶ Containing a single classfile, Module.php © All rights reserved. Zend Technologies, Inc.
  26. 26. Quick startZendSkeletonApplication © All rights reserved. Zend Technologies, Inc.
  27. 27. ZendSkeletonApplication● A simple, skeleton application using the ZF2 MVC layer and module systems● On GitHub: ▶ git clone --recursive git://github.com/zendframework/ZendSkeletonApplication.git● This project makes use of Git submodules● Works using ZF2.0.0beta3 © All rights reserved. Zend Technologies, Inc.
  28. 28. Folder tree config autoload application.config.php data module Application config vendor src public Application css Controller IndexController.php images view js Module.php .htaccess autoload_classmap.php index.php autoload_function.php autoload_register.php © All rights reserved. Zend Technologies, Inc.
  29. 29. Output © All rights reserved. Zend Technologies, Inc.
  30. 30. index.phpchdir(dirname(__DIR__));require_once (getenv(ZF2_PATH) ?:vendor/ZendFramework/library) ./Zend/Loader/AutoloaderFactory.php;ZendLoaderAutoloaderFactory::factory();$appConfig = include config/application.config.php;$listenerOptions = new ZendModuleListenerListenerOptions( $appConfig[module_listener_options]);$defaultListeners = newZendModuleListenerDefaultListenerAggregate($listenerOptions);$defaultListeners->getConfigListener() ->addConfigGlobPath("config/autoload/{,*.}{global,local}.config.php");... © All rights reserved. Zend Technologies, Inc.
  31. 31. index.php (2)...$moduleManager = new ZendModuleManager($appConfig[modules]);$moduleManager->events()->attachAggregate($defaultListeners);$moduleManager->loadModules();// Create application, bootstrap, and run$bootstrap = new ZendMvcBootstrap( $defaultListeners->getConfigListener()->getMergedConfig());$application = new ZendMvcApplication;$bootstrap->bootstrap($application);$application->run()->send(); © All rights reserved. Zend Technologies, Inc.
  32. 32. config/application.config.phpreturn array( modules => array( Application ), module_listener_options => array( config_cache_enabled => false, cache_dir => data/cache, module_paths => array( ./module, ./vendor, ), ),); © All rights reserved. Zend Technologies, Inc.
  33. 33. config/autoload● global.config.php● local.config.php.dist (.gitignore)● By default, this application is configured to load all configs in: ./config/autoload/{,*.}{global,local}.config.php.● Doing this provides a location for a developer to drop in configuration override files provided by modules, as well as cleanly provide individual, application-wide config files for things like database connections, etc. © All rights reserved. Zend Technologies, Inc.
  34. 34. Application/config/module.config.php return array( di => array( instance => array( … ) ) ); Here you configure the components of your application (i.e. routing, controller, view) © All rights reserved. Zend Technologies, Inc.
  35. 35. IndexController.phpnamespace ApplicationController;use ZendMvcControllerActionController, ZendViewModelViewModel;class IndexController extends ActionController{ public function indexAction() { return new ViewModel(); }} © All rights reserved. Zend Technologies, Inc.
  36. 36. Modules are “plug and play”● Easy to reuse a module, only 3 steps:1) Copy the module in module (or vendor) folder2) Enable the module in your application.config.php ▶ Add the name of the module in “modules”3) Copy the config file of the module in /config/autoload/module.<modules name>.config.php © All rights reserved. Zend Technologies, Inc.
  37. 37. modules.zendframework.com © All rights reserved. Zend Technologies, Inc.
  38. 38. Package system © All rights reserved. Zend Technologies, Inc.
  39. 39. Package system● We use Pyrus, a tool to manage PEAR packages. Pyrus simplifies and improves the PEAR experience.● Source packages (download + github): ▶ http://packages.zendframework.com/● Install and configure pyrus: ▶ 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.
  40. 40. From ZF1 to ZF2 © All rights reserved. Zend Technologies, Inc.
  41. 41. 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 possible options (one is ZF1) ▶ MVC: module, event based, dispatchable © All rights reserved. Zend Technologies, Inc.
  42. 42. 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 ZF1s Zend_Application bootstrap works.● The default route utilizes the new ZF2 MVC routing, but mimics what ZF1 provided. © All rights reserved. Zend Technologies, Inc.
  43. 43. 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.
  44. 44. Questions? © All rights reserved. Zend Technologies, Inc.
  45. 45. Thank you!● Comments and feedbacks: – Email: enrico@zend.com – Twitter: @ezimuel © All rights reserved. Zend Technologies, Inc.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×