{"id":173,"date":"2012-09-01T22:00:30","date_gmt":"2012-09-01T20:00:30","guid":{"rendered":"http:\/\/codingexplained.com\/?p=173"},"modified":"2017-06-11T22:33:50","modified_gmt":"2017-06-11T20:33:50","slug":"module-based-forms-directory","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory","title":{"rendered":"Module Based Forms Directory"},"content":{"rendered":"<p>When using a modular structure in a Zend Framework project, it is nice to have the form classes located within a folder in the module to which they belong. They <em>could<\/em> technically be added to the library folder and accessed by adding the namespace to the auto loader, but it is preferred to keep application dependent code within the <span class=\"code\">application<\/span> folder &#8211; and even better, in the respective modules&#8217; folders. Luckily, this is, as we shall see, a fairly trivial task.<\/p>\n<h3>Updating the Configuration File<\/h3>\n<p>Begin by opening the <span class=\"code\">application\/configs\/application.ini<\/span> file. Under the <span class=\"code\">[production]<\/span> section, make sure you have the following two lines:<\/p>\n<pre><code class=\"ini\">resources.frontController.moduleDirectory = APPLICATION_PATH &quot;\/modules&quot;\r\nresources.modules[] = <\/code><\/pre>\n<p>Strange as it looks, the second name <em>is<\/em> supposed to look like that.<\/p>\n<h3>Creating the Forms Folder and Form Class<\/h3>\n<p>Assuming that you have your <a href=\"\/coding\/php\/zend-framework\/setting-up-for-a-modular-directory-structure\" target=\"_blank\" title=\"Setting up a modular directory structure\">module directories all set up<\/a>, create the following folder:<\/p>\n<pre><code>application\/modules\/default\/forms<\/code><\/pre>\n<p>Once this has been done, create a file with your desired name within this folder. The name should not contain underscores and should be in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Pascal_case\" title=\"Pascal case on Wikipedia.org\" target=\"_blank\">Pascal case<\/a> (camel case with a first uppercase letter). For instance, say we want a form so that our users can register on our site. It would then be reasonable to name this form&#8217;s file <span class=\"code\">Register.php<\/span>.<\/p>\n<p>For the class name, one must use a specific naming convention so that the Zend Framework knows where to find the class. It must be of the following format: <span class=\"code\">[module name]_Form_[form name]<\/span>. So, for a form with the name <span class=\"code\">Register<\/span> in the <span class=\"code\">Default<\/span> module, the class name would be <span class=\"code\">Default_Form_Register<\/span>. Be sure to use the part after the last underscore (<span class=\"code\">Register<\/span> in this case) as the file name with <span class=\"code\">.php<\/span> appended. This is because of the <span class=\"code\">Zend_Application_Module_Autoloader<\/span>&#8216;s <a href=\"http:\/\/framework.zend.com\/manual\/en\/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.module\" title=\"Zend_Application_Module_Autoloader default mappings\" target=\"_blank\">default mappings<\/a>.<\/p>\n<pre><code class=\"php\">class Default_Form_Register extends Zend_Form { }<\/code><\/pre>\n<h3>Adding a Bootstrap Class<\/h3>\n<p>Before we are ready to use the form, there is just one more thing to do; one which many people find strange. We need to create a bootstrap file for our module, so create the below file.<\/p>\n<pre><code>application\/modules\/default\/Bootstrap.php<\/code><\/pre>\n<p>This file and class are required for this to work, but the class can just be left empty. Paste the below code into the new bootstrap file.<\/p>\n<pre><code class=\"php\">class Default_Bootstrap extends Zend_Application_Module_Bootstrap { }<\/code><\/pre>\n<p>Remember the PHP opening tag at the top of the file. As you might have noticed, the class name should be of the format <span class=\"code\">[module name]_Bootstrap<\/span>.<\/p>\n<h3>Instantiating the Form<\/h3>\n<p>Now we are ready to instantiate this form in a controller. Simply do like below.<\/p>\n<pre><code class=\"php\">class UserController extends Zend_Controller_Action {\r\n\tpublic function registerAction() {\r\n\t\t$this-&gt;view-&gt;form = new Default_Form_Register();\r\n\t}\r\n}<\/code><\/pre>\n<p>That is all there is to it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keeping form classes together in each module is the preferred way to store them, rather than in the library folder, for instance. Keeping the Zend Framework naming conventions in mind, this is a very easy task.<\/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>Module Based Forms Directory in Zend Framework<\/title>\n<meta name=\"description\" content=\"With a modular directory structure in Zend Framework, form classes are preferred to be within each of the modules. In this article, we show how to do this.\" \/>\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\/module-based-forms-directory\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Module Based Forms Directory in Zend Framework\" \/>\n<meta property=\"og:description\" content=\"With a modular directory structure in Zend Framework, form classes are preferred to be within each of the modules. In this article, we show how to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory\" \/>\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-01T20:00:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:33:50+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\/module-based-forms-directory\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory\",\"name\":\"Module Based Forms Directory in Zend Framework\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2012-09-01T20:00:30+00:00\",\"dateModified\":\"2017-06-11T20:33:50+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"With a modular directory structure in Zend Framework, form classes are preferred to be within each of the modules. In this article, we show how to do this.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Module Based Forms Directory\"}]},{\"@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":"Module Based Forms Directory in Zend Framework","description":"With a modular directory structure in Zend Framework, form classes are preferred to be within each of the modules. In this article, we show how to do this.","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\/module-based-forms-directory","og_locale":"en_US","og_type":"article","og_title":"Module Based Forms Directory in Zend Framework","og_description":"With a modular directory structure in Zend Framework, form classes are preferred to be within each of the modules. In this article, we show how to do this.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2012-09-01T20:00:30+00:00","article_modified_time":"2017-06-11T20:33:50+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\/module-based-forms-directory","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory","name":"Module Based Forms Directory in Zend Framework","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2012-09-01T20:00:30+00:00","dateModified":"2017-06-11T20:33:50+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"With a modular directory structure in Zend Framework, form classes are preferred to be within each of the modules. In this article, we show how to do this.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/module-based-forms-directory#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Module Based Forms Directory"}]},{"@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-2N","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/173"}],"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=173"}],"version-history":[{"count":19,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":3058,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/173\/revisions\/3058"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=173"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}