ZF2 Modular Architecture - Taking advantage of it

5,823 views
5,649 views

Published on

Zend Framework 1 had modules. Their name is pretty much everything Zend Framework 2 modules share with them, though. The whole framework architecture has been rewritten to encourage software reuse and extension. In this talk I’ll share our experiences on ZF2 module creation and usage.

Published in: Technology
3 Comments
16 Likes
Statistics
Notes
  • Hey, that's a good stuff, I like it. I have also introduced my own implementation of "Onion Architecture" with ZF2. I have rid of the "module" directory and instead all the modules are placed in "solution" folder where modules organized in layers. Check it out here http://blog.igorvorobiov.com/2014/12/21/onion-architecture-and-zend-framework-2/ and let me know what you think.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sorry for the late reply, missed your msg. Feel free to use the contents of all of my slide decks in your presentations. They're all under CC Attribution-NonCommercial license.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi ! I have to give a presentation on ZF2 in my company. I like your presentation very much. It is just a perfect presentation for ZF2. Can I copy some of the page contents for my presentation?

    Thanks !
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,823
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
200
Comments
3
Likes
16
Embeds 0
No embeds

No notes for slide

ZF2 Modular Architecture - Taking advantage of it

  1. 1. ZF2 Modular Architecture Taking Advantage of It PHPDay – Verona, Italy – 18/05/2013
  2. 2. 2 STEVE MARASPIN
  3. 3. 3 http://friuli.grusp.org/
  4. 4. MODULES, WHAT? Picture credits: http://www.flickr.com/photos/10459273@N05/4771563267
  5. 5. A Module is… 5 A collection of code and other files that solves a more specific atomic problem of the larger business problem. ZF2 RFC
  6. 6. A re-usable piece of functionality that can be used to construct a more complex application. 6 Evan Coury - http://evan.pro/zf2-modules-talk.html A Module is…
  7. 7. A re-usable piece of functionality that can be used to construct a more complex application. 7 Evan Coury - http://evan.pro/zf2-modules-talk.html A Module is…
  8. 8. 8
  9. 9. • 15.Reuse-in-the-small is a solved problem. • 16.Reuse-in-the-large remains a mostly unsolved problem. 9 Robert R. Glass – Facts and Fallacies of Software Engineering About Software Reuse
  10. 10. What I remember about ZF1 Modules… 10 Model Admin Panel content management, store management, contact requests, ... Web Site contents, product catalogue, contact form, …
  11. 11. What could we reuse? 11 Model Admin Panel content management, store management, contact requests, ... Web Site contents, product catalogue, contact form, …
  12. 12. Picture credits: http://www.flickr.com/photos/cclark395/7671665642/ MEET THE NICE FOLKS AT OUR COMPANY
  13. 13. INFORMATION DESK
  14. 14. TECHNICAL OFFICE Picture credits: http://www.flickr.com/photos/ter-burg/5807937726/
  15. 15. WAREHOUSE Picture credits: http://www.flickr.com/photos/forklifttrainingadelaide/6774324457/
  16. 16. ACCOUNT MANAGER
  17. 17. 17 Our Company Hierarchy TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR. INFORMATION DESK DEPARTMENT ONE DEPARTMENT TWO
  18. 18. 18 Customer arrives… TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  19. 19. 19 Information desk greets her TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  20. 20. 20 …gives her directions… WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR. TECHNICAL OFFICE ACCOUNT MGR.
  21. 21. 21 Warehouse folks do things TECHNICAL OFFICE ACCOUNT MGR. TECHNICAL OFFICE WAREHOUSE ACCOUNT MGR.
  22. 22. 22 Stuff is ready for delivery TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  23. 23. 23 Customer is happy. TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  24. 24. 24 Customer is happy. Eventually. TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  25. 25. 25 But Hey…
  26. 26. This is MVC! 26 But Hey…
  27. 27. 27 http://framework.zend.com/
  28. 28. 28 Our Application CONTROLLER VIEW MODEL CONTROLLER VIEW
  29. 29. 29 Back to our company… TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR. INFORMATION DESK
  30. 30. 30 We need to move a dept… TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  31. 31. 31 Dependency problem! TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR.
  32. 32. 32 In our application… CONTROLLER VIEW MODEL CONTROLLER VIEW
  33. 33. 33 Same Problem! CONTROLLER VIEW MODEL CONTROLLER VIEW
  34. 34. 34 Solving company problem… TECHNICAL OFFICE ACCOUNT MGR. WAREHOUSE TECHNICAL OFFICE ACCOUNT MGR. INFORMATION DESK WAREHOUSE
  35. 35. 35 Same with our application… CONTROLLER VIEW MODEL CONTROLLER VIEW MODEL
  36. 36. 36 Project delivered!
  37. 37. 37 Let’s start a new one…
  38. 38. 38 Hey, we need this thing!
  39. 39. 39 No problems, folks! MODEL CONTROLLER VIEW
  40. 40. 40 http://framework.zend.com/
  41. 41. WELCOME ZF2 MODULES Picture Credits: http://www.flickr.com/photos/10459273@N05/4771563267
  42. 42. 42 Namespace Module.php Module
  43. 43. 43 Namespace MioModulo; Class Module {} Namespace Module.php Module Simplest Module:
  44. 44. WHAT COULD WE DO WITH IT? Picture credits: http://www.flickr.com/photos/vittoriomilanes/8286559013
  45. 45. 45 Format a date Handle a contact form
  46. 46. 46 Format a date Handle a contact form Handle ACLs Integrate an ORM
  47. 47. 47 Format a date Handle a contact form Handle ACLs Integrate an ORM Encapsulate a whole application
  48. 48. WHAAAT? Picture credits: http://www.flickr.com/photos/elzey/3481161467/
  49. 49. 49 Format a date Handle a contact form Handle ACLs Integrate an ORM Encapsulate a whole application
  50. 50. Let’s think about ZF1 once again… 50 Model Admin Panel content management, store management, contact requests, ... Web Site contents, product catalogue, contact form, …
  51. 51. This is how modules are now… 51 Web Site Model Admin Panel Content Management
  52. 52. Focus on features 52 Web Site Model Admin Panel Product Catalogue Contact Form Content Management
  53. 53. Focus on features 53 Website Model Admin Panel Product Catalogue Contact Form Content Management
  54. 54. What’s in a Module? 54 Feature
  55. 55. What’s in a module? 55 Feature Controller Views PHP Classes Routing Configuration …
  56. 56. Business Logic Hints 56 Feature Controller Viste PHP Classes Routing Configurazioni … Services Helpers / Plugins Mappers …
  57. 57. LET’S GET OUR HANDS ON IT… Picture credits: http://www.flickr.com/photos/vittoriomilanes/8286559013
  58. 58. THE SKELETON APP Picture credits: http://www.flickr.com/photos/fil/144232588/
  59. 59. 59 Cloning git clone https://github.com/zendframework/ZendSkeletonApplication
  60. 60. 60 http://getcomposer.org/
  61. 61. 61 composer.json { "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.*" } }
  62. 62. 62 composer.json { "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.*" } }
  63. 63. 63 Installation cd ZendSkeletonApplication php composer.phar self-update php composer.phar install
  64. 64. 64 Installation cd ZendSkeletonApplication php composer.phar self-update php composer.phar install > Installing zendframework/zendframework
  65. 65. 66 Filesystem Layout
  66. 66. 67 Filesystem Layout Application Wide Configuration
  67. 67. 68 Filesystem Layout Misc (cached files, documents …)
  68. 68. 69 Filesystem Layout Public DocRoot
  69. 69. 70 Filesystem Layout Our Modules
  70. 70. Filesystem Layout 71 Third Party Modules
  71. 71. Focusing on a module… 72 Module specific configuration
  72. 72. What’s inside a module? 73 Module PHP Source Files (Controller, Form, Model, Services, …)
  73. 73. What’s inside a module? 74 Module test files
  74. 74. What’s inside a module? 75 View stuff (view files, layouts, partials)
  75. 75. LET’S BOOTSTRAP SH*T UP! Picture credits: http://www.flickr.com/photos/fil/144232588/
  76. 76. index.php <?php /** * This makes our life easier when dealing with paths. Everything is * relative to the application root now. */ chdir(dirname(__DIR__)); // Setup autoloading require 'init_autoloader.php'; // Run the application! ZendMvcApplication::init(require 'config/application.config.php')->run();
  77. 77. index.php <?php /** * This makes our life easier when dealing with paths. Everything is * relative to the application root now. */ chdir(dirname(__DIR__)); // Setup autoloading require 'init_autoloader.php'; // Run the application! ZendMvcApplication::init(require 'config/application.config.php')->run();
  78. 78. config/application.config.php 79 return array( 'modules' => array( 'DoctrineModule', 'DoctrineORMModule', 'SpeckPaypal', 'Application', ), ), ...
  79. 79. Module.php namespace Application; use ZendMvcModuleRouteListener; use ZendMvcMvcEvent; class Module { public function onBootstrap(MvcEvent $e) {[…]} public function getConfig() { return include __DIR__ . '/config/module.config.php'; } public function getAutoloaderConfig() {} } 80
  80. 80. Module configuration file // config/module.config.php return array( 'router' => array( 'routes' => array(...) ), 'controllers' => array(...), 'service_manager' => array(...), 'view_manager' => array(...), ... )
  81. 81. Where’s module configuration? 82 Here!
  82. 82. Controllers <?php namespace ApplicationController; use ZendMvcControllerAbstractActionController; use ZendViewModelViewModel; class IndexController extends AbstractActionController { public function indexAction() { $timestamp = time(); return new ViewModel(array( 'timestamp' => $timestamp )); } }
  83. 83. Actions <?php namespace ApplicationController; use ZendMvcControllerAbstractActionController; use ZendViewModelViewModel; class IndexController extends AbstractActionController { public function indexAction() { $timestamp = time(); return new ViewModel(array( 'timestamp' => $timestamp )); } }
  84. 84. CONVENTION OVER CONFIGURATION Picture credits: http://www.flickr.com/photos/29233640@N07/8412347937/
  85. 85. This is for all modules 86 return array( 'modules' => array( 'DoctrineModule', 'DoctrineORMModule', 'SpeckPaypal', 'Application', ), ), ... config/application.config.php
  86. 86. 87 Feature A Controller Views PHP Classes Routing Configurations …
  87. 87. 88 Feature A Controller Views PHP Classes Routing Configurations … Feature B Controller Views PHP Classes Routing Configurations … Feature C Controller Views PHP Classes Routing Configurations …
  88. 88. 89 Controller Views PHP Classes Routing Configurations … Controller Views PHP Classes Routing Configurations … Controller Views PHP Classes Routing Configurations … Modules are merged at runtime
  89. 89. CONFIGURATION MERGE Picture credits: http://www.flickr.com/photos/michelleundihrefotos/5970015736
  90. 90. 91 Controller Views Routing … Controller Views Routing … Controller Views Routing … Modules are merged at runtime PHP Classes PHP Classes PHP Classes Configurations
  91. 91. We can override things… Views / Layouts 92 return array( ... 'view_manager' => array( ... ), )
  92. 92. We can override things… Views / Layouts Routing Rules 93 return array( ... 'router' => array( ... ), )
  93. 93. We can override things… Views / Layouts Routing Rules All Other Configuration Parameters 94
  94. 94. We can override things… Views / Layouts Routing Rules All Other Configuration Parameters 95 Do NOT modify original 3° party modules!
  95. 95. Custom Configuration 96 Do not touch anything here! MODULE.local.php
  96. 96. 97 Controller Views Routing … Controllers Views Routing … Controller Views Routing … Modules are merged at runtime PHP Classes PHP Classes PHP Classes Configurations Configurations Configurations
  97. 97. This happens for all modules 98 return array( 'modules' => array( 'DoctrineModule', 'DoctrineORMModule', 'SpeckPaypal', 'Application', ), ), ... config/application.config.php Order is Important!
  98. 98. LET’S LOOK AROUND FIRST! Picture credits: http://www.flickr.com/photos/billward/5626976800
  99. 99. http://modules.zendframework.com/ 100
  100. 100. What about a contact form? 101
  101. 101. What about a contact form? 10 2
  102. 102. cd vendor git clone https://github.com/weierophinney/PhlyContact.git 10 3
  103. 103. 10 4 Here we go…
  104. 104. application.config.php <?php return array( 'modules' => array( 'PhlyContact', 'Application', ), 'module_listener_options' => array( […] ), ); 105
  105. 105. application.config.php <?php return array( 'modules' => array( 'PhlyContact', 'Application', ), 'module_listener_options' => array( […] ), ); 106
  106. 106. 10 7 Default Configuration
  107. 107. PhlyContact/config/module.config.php <?php return array( 'phly_contact' => array( […] 'message' => array( 'to' => array('EMAIL HERE' => 'NAME HERE',), 'sender' => array( 'address' => 'EMAIL HERE', 'name' => 'NAME HERE', ), 'from' => array('EMAIL HERE' => 'NAME HERE',), ), […] ), 108
  108. 108. 10 9 Remember: Do NOT modify files in here!
  109. 109. 110 DO create and use files herein instead
  110. 110. In less than 2 minutes… 111
  111. 111. BUILDING OUR OWN MODULES Picture credits: http://www.flickr.com/photos/pullip_junk/6624713631
  112. 112. SHARED RESOURCES Picture credits: http://www.flickr.com/photos/nasahqphoto/5162244810
  113. 113. Catalogue 114 Shopping Cart
  114. 114. 115 Product Catalogue Shopping Cart Where shall we put this? - id - name - price
  115. 115. Little Information Needed 116
  116. 116. 117 Shopping Cart Product - id - name - price
  117. 117. We need a bit more stuff here… 118
  118. 118. Catalogue 119 Shopping Cart Product - id - name - price Product - id - name - price - description - picturePath
  119. 119. Catalogue 12 0 Shopping Cart - description - picturePath - … Product - id - name - price Product - id - name - price
  120. 120. 121 - description - picturePath - … Product - id - name - price Catalogue Shopping Cart
  121. 121. /** * CatalogueEntityProduct * ... */ class Product extends EcommerceEntityCartproduct {} Through extension /** * CatalogueEntityProduct * ... */ class Product implements EcommerceEntityCartproductInterface {} Through Interfaces Constraints
  122. 122. How do I make this possible? 12 3
  123. 123. SERVICE MANAGER Picture credits: http://www.flickr.com/photos/urbanwoodswalker/4375401057/
  124. 124. Service Locator SL Product Controller ShoppingCart
  125. 125. NOT THE REGISTRY
  126. 126. In a Controller Factory <?php public function createService(ServiceLocatorInterface $services){ $serviceLocator = $services->getServiceLocator(); $cartService = $serviceLocator->get('CartItem'); $controller = new CartController($cartService); return $controller; } }
  127. 127. Original Module Configuration /* Shopping Cart Module – module.config.php */ return array( 'router' => array(…), 'controllers' => array(…), 'service_manager' => array( 'invokables' => array( 'CartItem' => 'CartServiceCartItem', ), 'factories' => array(…) ) );
  128. 128. Module Configuration /* Shopping Cart Module – module.config.php */ return array( 'router' => array(…), 'controllers' => array(…), 'service_manager' => array( 'invokables' => array( // 'CartItem' => 'CartServiceCartItem', 'CartItem' => 'CatalogueServiceCatalogueItem', ), 'factories' => array(…) ) );
  129. 129. 13 0 LSP in Action Catalogue Shopping Cart Product - id - name - price Product - id - name - price - description - picturePath
  130. 130. 131 Catalogue Shopping Cart Product - id - name - price - description - picturePath Product - id - name - price - description - picturePath
  131. 131. REUSE ENABLED Picture credits: http://www.flickr.com/photos/89544908@N00/7877685112/
  132. 132. REUSE ENABLED EVEN IN THE LARGE Picture credits: http://www.flickr.com/photos/89544908@N00/7877685112/
  133. 133. 134
  134. 134. A re-usable piece of functionality that can be used to construct a more complex application. 135 Evan Coury - http://evan.pro/zf2-modules-talk.html Back to definition
  135. 135. A re-usable piece of functionality that can be used to construct a more complex application. 136 Evan Coury - http://evan.pro/zf2-modules-talk.html Now I understand it better!
  136. 136. I hope you too. Stefano Maraspin @maraspin s.maraspin@mvassociati.it
  137. 137. QUESTIONS? Picture credits: http://www.flickr.com/photos/hefhoover/3929261255/
  138. 138. 139 @maraspin
  139. 139. 140 http://www.mvlabs.it/
  140. 140. Stefano Maraspin @maraspin s.maraspin@mvassociati.it http://joind.in/talk/view/8655

×