{"id":496,"date":"2013-01-17T17:03:38","date_gmt":"2013-01-17T16:03:38","guid":{"rendered":"http:\/\/codingexplained.com\/?p=496"},"modified":"2017-06-11T22:11:15","modified_gmt":"2017-06-11T20:11:15","slug":"zend-framework-2-module-manager","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager","title":{"rendered":"Zend Framework 2 Module Manager"},"content":{"rendered":"<p>In this article, we will look at Zend Framework 2&#8217;s module system and why it is such a central part of Zend Framework 2. Afterwards, we will go into more details in terms of the Module Manager and briefly discuss how it works along with autoloading.<\/p>\n<h3>The Module System<\/h3>\n<p>One of the key features of Zend Framework 2, in comparison with its predecessor, is the module system. Its purpose is to enable developers to write self-contained modules which are easy to plug into existing applications and hence greatly increase code reuse. While Zend Framework 1 did allow the separation of code into modules, it was usually not without significant effort to integrate a third party module into an application.<\/p>\n<p>In Zend Framework 2, each module provides its own configuration files which are then merged together with the entire application&#8217;s configuration files by the module manager. As a result, the configuration required for a module to function is provided within the module itself, and therefore there is no need to spend time configuring an application&#8217;s configuration files in order for the module to integrate with the application.<\/p>\n<p>For instance, most applications will need to provide some basic functionality; registering, logging in, logging out, recovering passwords, etc. This related functionality would be suitable for a &#8220;User&#8221; module which could then be reused across development projects.<\/p>\n<p>Another example could be that a website needs a blog. Rather than developing one&#8217;s own blog, one can make use of existing modules that are available. This saves both time and money. The alternative could be to use a parallel blogging system such as WordPress. However, it is easy to imagine the kind of challenges this would bring; if the blog has to integrate with the existing caching or logging system, for instance, this would be complicated. Also, the blog&#8217;s data would most likely be stored in a separate database, and if the latest blog posts were to be displayed on the application&#8217;s front page, this could prove to be a challenge. This functionality would have to make use the blog&#8217;s API, feed or database. Another challenge could be if the layouts were to be aligned; the design would have to be maintained in parallel. If a module could be used, all of these integration challenges could be avoided.<\/p>\n<h3>The Module Manager<\/h3>\n<p>The module manager is responsible for loading the project&#8217;s modules by iterating through those defined in <span class=\"code\">config\/application.config.php<\/span>. When doing this, it triggers a sequence of events for each module. Therefore, very little actually happens within the <span class=\"code\">ModuleManager<\/span> class itself; the <span class=\"code\">loadModules()<\/span> method simple triggers events so that the processing happens in the event listeners.<\/p>\n<p>A detailed walkthrough of what happens internally is outside the scope of this article, but what basically happens is that the module classes found in <span class=\"code\">Module.php<\/span> are instantiated and various configuration methods are invoked. These methods could be for configuring the service manager, autoloading, controllers, controller plugins, routing and the view manager. The configuration for each module is merged into the main application configuration.<\/p>\n<h4>Autoloading<\/h4>\n<p>Zend Framework 2 makes use of namespaces which were introduced in PHP 5.3.0. However, namespaces do not remove the need to load necessary files, something that has previously been done with <span class=\"code\">require()<\/span> or <span class=\"code\">include()<\/span>. As a result, the framework includes autoloading functionality such that this happens automatically. The only thing module developers have to do is to specify how the autoloading should be done. This can be as simple as using <span class=\"code\">Zend\\Loader\\StandardAutoloader<\/span>. Within a module class, this can be configured like below.<\/p>\n<pre><code class=\"php\">public function getAutoloaderConfig() {\r\n\treturn array(\r\n\t\t'Zend\\Loader\\StandardAutoloader' => array(\r\n\t\t\t'namespaces' => array(\r\n\t\t\t\t__NAMESPACE__ => __DIR__ . '\/src\/' . __NAMESPACE__\r\n\t\t\t)\r\n\t\t)\r\n\t);\r\n}<\/code><\/pre>\n<p>The module manager will invoke this method if the class either implements <span class=\"code\">Zend\\ModuleManager\\Feature\\AutoloaderProviderInterface<\/span> or simply has a method with a name of <span class=\"code\">getAutoloaderConfig()<\/span>. In this case, the standard autoloader is simply given a path where the current namespace&#8217;s files can be found. The magic constants refer to the current namespace name and directory path, respectively.<\/p>\n<p>What happens internally is that an autoload method is registered with PHP&#8217;s <span class=\"code\">spl_autoload_register()<\/span> method. This method adds the callable passed as a parameter to the autoload stack such that it is invoked if a class that has not been loaded is attempted to be used. Thus, autoloading lazily loads required files. This means that developers to not have to use <span class=\"code\">require()<\/span> or <span class=\"code\">include()<\/span> calls; rather, they can simply access classes directly and then the autoloading will take care of the rest.<\/p>\n<p>During development, the standard autoloader is sufficient, but for a production environment where performance is more critical, the <span class=\"code\">Zend\\Loader\\ClassMapAutoloader<\/span> should be used. This approach can be configured like below.<\/p>\n<pre><code class=\"php\">public function getAutoloaderConfig() {\r\n\treturn array(\r\n\t\t'Zend\\Loader\\ClassMapAutoloader' => array(\r\n\t\t\t__DIR__ . '\/autoload_classmap.php'\r\n\t\t)\r\n\t);\r\n}<\/code><\/pre>\n<p>By convention, the <span class=\"code\">ClassMapAutoloader<\/span> should use a file named <span class=\"code\">autoload_classmap.php<\/span>. This file should return an array of class name and fully qualified file name pairs. This ensures that autoloading can be very efficient by simply doing an array lookup, which has an efficiency of O(1) in the Big O notation.<\/p>\n<p>Multiple approaches can be used simultaneously; because the <span class=\"code\">getAutoloaderConfig()<\/span> method returns an array, these two approaches can be combined. A sensible approach would then be to first try to load a class with the efficient <span class=\"code\">ClassMapAutoloader<\/span> and as a fallback, attempt to load it with the less efficient <span class=\"code\">StandardAutoloader<\/span>. This way, an error can potentially be avoided if an entry is missing in the class map, for instance.<\/p>\n<p>In addition to the class map file, two other files can also be added; <span class=\"code\">autoload_function.php<\/span> and <span class=\"code\">autoload_register.php<\/span>. The former should return a callback function which can be used together with <span class=\"code\">spl_autoload_register()<\/span> and may utilize the class map. The latter should register this callback function \u2014 typically with <span class=\"code\">spl_autoload_register()<\/span>. The reason for all of this is flexibility. By supplying these three files, autoloading can quickly and easily be configured, even if parts of the module is not used within a Zend Framework application \u2014 e.g. reusing the module layer in another context.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most central concepts of Zend Framework 2 is the Module Manager. This article gives an overview of how it works and discusses how autoloading is used to simplify the code within modules.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[38],"tags":[40,39,66],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Zend Framework 2 Module Manager<\/title>\n<meta name=\"description\" content=\"The Module Manager in Zend Framework 2 is a central part of the framework. This article discusses how it works along with how autoloading is used.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Zend Framework 2 Module Manager\" \/>\n<meta property=\"og:description\" content=\"The Module Manager in Zend Framework 2 is a central part of the framework. This article discusses how it works along with how autoloading is used.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-17T16:03:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:11:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager\",\"name\":\"Zend Framework 2 Module Manager\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2013-01-17T16:03:38+00:00\",\"dateModified\":\"2017-06-11T20:11:15+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"The Module Manager in Zend Framework 2 is a central part of the framework. This article discusses how it works along with how autoloading is used.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Zend Framework 2 Module Manager\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Zend Framework 2 Module Manager","description":"The Module Manager in Zend Framework 2 is a central part of the framework. This article discusses how it works along with how autoloading is used.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager","og_locale":"en_US","og_type":"article","og_title":"Zend Framework 2 Module Manager","og_description":"The Module Manager in Zend Framework 2 is a central part of the framework. This article discusses how it works along with how autoloading is used.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2013-01-17T16:03:38+00:00","article_modified_time":"2017-06-11T20:11:15+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager","name":"Zend Framework 2 Module Manager","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2013-01-17T16:03:38+00:00","dateModified":"2017-06-11T20:11:15+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"The Module Manager in Zend Framework 2 is a central part of the framework. This article discusses how it works along with how autoloading is used.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/zend-framework-2-module-manager#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Zend Framework 2 Module Manager"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-80","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/496"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":5,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":3031,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/496\/revisions\/3031"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=496"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}