{"id":196,"date":"2012-09-01T17:20:20","date_gmt":"2012-09-01T15:20:20","guid":{"rendered":"http:\/\/codingexplained.com\/?p=196"},"modified":"2017-06-11T22:32:33","modified_gmt":"2017-06-11T20:32:33","slug":"setting-up-for-a-modular-directory-structure","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure","title":{"rendered":"Setting up for a Modular Directory Structure"},"content":{"rendered":"<p>Using a modular directory structure allows code to be separated into self-contained units. It can be thought of as independent smaller MVC applications within your main application. For instance, a website may have a <span class=\"code\">blog<\/span> and <span class=\"code\">admin<\/span> module besides the <span class=\"code\">default<\/span> module. When creating a new project by using the <a href=\"http:\/\/framework.zend.com\/manual\/en\/zend.tool.framework.clitool.html\" title=\"Zend Framework CLI Tool documentation\" target=\"_blank\">Zend Framework CLI Tool<\/a>, a modular structure is not present by default. In this article we will show how to change this by changing the project&#8217;s configuration file.<\/p>\n<h3>Editing the Configuration File<\/h3>\n<p>The first thing to do is to change the <span class=\"code\">application\/configs\/application.ini<\/span> file so that the framework knows to use a modular structure and where to find the modules. In the <span class=\"code\">[production]<\/span> section, add the two lines below.<\/p>\n<pre><code class=\"ini\">resources.frontController.moduleDirectory = APPLICATION_PATH &quot;\/modules&quot;\r\nresources.modules[] = <\/code><\/pre>\n<h3>Creating the Required Directories<\/h3>\n<p>Now we need to create the required directories. First, we need to create the directory which will contain our modules; <span class=\"code\">modules<\/span> as per our configuration file. Create the following two directories:<\/p>\n<pre><code>\r\napplication\/modules\r\napplication\/modules\/default<\/code><\/pre>\n<p>Now we have created the directory for the <span class=\"code\">default<\/span> module, but we need to add folders within it to contain models, views and controllers. If you used the Zend Framework CLI Tool to create your project, you can simply cut and paste the <span class=\"code\">models<\/span>, <span class=\"code\">views<\/span> and <span class=\"code\">controllers<\/span> folders that were automatically created within the <span class=\"code\">application<\/span> folder. Otherwise you will have to create the folders manually like discussed in the section below.<\/p>\n<h4>Creating the Directories By Hand<\/h4>\n<p>If you are creating your project by hand, then create the below folders.<\/p>\n<pre><code>application\/modules\/default\/models\r\napplication\/modules\/default\/views\r\napplication\/modules\/default\/views\/scripts\r\napplication\/modules\/default\/views\/scripts\/index\r\napplication\/modules\/default\/controllers<\/code><\/pre>\n<p>If you manually created the above folders, then you need to create a controller and a view script. Create the file <span class=\"code\">application\/modules\/default\/controllers\/IndexController.php<\/span> and fill it with the code below.<\/p>\n<pre><code class=\"php\">class IndexController extends Zend_Controller_Action {\r\n\tpublic function indexAction() {\r\n\t\r\n\t}\r\n}<\/code><\/pre>\n<p>The <span class=\"code\">indexAction<\/span> will automatically look for the view script <span class=\"code\">application\/modules\/default\/views\/scripts\/index\/index.phtml<\/span>, so create that file with your desired HTML content. Also note that for exceptions (e.g. incorrect URLs), you should consider creating an <span class=\"code\">ErrorController<\/span> along with a view script to handle these gracefully.<\/p>\n<h3>Testing the Modular Application<\/h3>\n<p>Now that everything is ready, it is time to see the modular application in action. Try to visit your website&#8217;s front page. Entering <span class=\"code\">localhost<\/span> (or wherever your site is located) into your browser&#8217;s address bar should show you the front page. Behind the scenes, the router is automatically routing the request to <span class=\"code\">your-domain\/default\/index\/index<\/span>. Enter this in your address bar and you should see the same page.<\/p>\n<p>Congratulations, you are now ready to develop modular applications in the Zend Framework!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using a modular directory structure can logically separate parts of an application. By default, a Zend Framework application does not make use of such a structure, so in this article we shall see how to easily change this.<\/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],"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>Setting up for a Modular Directory Structure<\/title>\n<meta name=\"description\" content=\"Using a modular directory structure can logically separate parts of an application. The Zend Framework does not do this as default, so we will show you how.\" \/>\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\/setting-up-for-a-modular-directory-structure\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting up for a Modular Directory Structure\" \/>\n<meta property=\"og:description\" content=\"Using a modular directory structure can logically separate parts of an application. The Zend Framework does not do this as default, so we will show you how.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure\" \/>\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=\"2012-09-01T15:20:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:32:33+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=\"2 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\/setting-up-for-a-modular-directory-structure\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure\",\"name\":\"Setting up for a Modular Directory Structure\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2012-09-01T15:20:20+00:00\",\"dateModified\":\"2017-06-11T20:32:33+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Using a modular directory structure can logically separate parts of an application. The Zend Framework does not do this as default, so we will show you how.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting up for a Modular Directory Structure\"}]},{\"@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":"Setting up for a Modular Directory Structure","description":"Using a modular directory structure can logically separate parts of an application. The Zend Framework does not do this as default, so we will show you how.","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\/setting-up-for-a-modular-directory-structure","og_locale":"en_US","og_type":"article","og_title":"Setting up for a Modular Directory Structure","og_description":"Using a modular directory structure can logically separate parts of an application. The Zend Framework does not do this as default, so we will show you how.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2012-09-01T15:20:20+00:00","article_modified_time":"2017-06-11T20:32:33+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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure","name":"Setting up for a Modular Directory Structure","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2012-09-01T15:20:20+00:00","dateModified":"2017-06-11T20:32:33+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Using a modular directory structure can logically separate parts of an application. The Zend Framework does not do this as default, so we will show you how.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Setting up for a Modular Directory Structure"}]},{"@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-3a","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/196"}],"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=196"}],"version-history":[{"count":16,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":3057,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/196\/revisions\/3057"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=196"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}