Module Based Forms Directory

Published on September 1, 2012 by

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 could 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 application folder – and even better, in the respective modules’ folders. Luckily, this is, as we shall see, a fairly trivial task.

Updating the Configuration File

Begin by opening the application/configs/application.ini file. Under the [production] section, make sure you have the following two lines:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = 

Strange as it looks, the second name is supposed to look like that.

Creating the Forms Folder and Form Class

Assuming that you have your module directories all set up, create the following folder:

application/modules/default/forms

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 Pascal case (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’s file Register.php.

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: [module name]_Form_[form name]. So, for a form with the name Register in the Default module, the class name would be Default_Form_Register. Be sure to use the part after the last underscore (Register in this case) as the file name with .php appended. This is because of the Zend_Application_Module_Autoloader‘s default mappings.

class Default_Form_Register extends Zend_Form { }

Adding a Bootstrap Class

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.

application/modules/default/Bootstrap.php

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.

class Default_Bootstrap extends Zend_Application_Module_Bootstrap { }

Remember the PHP opening tag at the top of the file. As you might have noticed, the class name should be of the format [module name]_Bootstrap.

Instantiating the Form

Now we are ready to instantiate this form in a controller. Simply do like below.

class UserController extends Zend_Controller_Action {
	public function registerAction() {
		$this->view->form = new Default_Form_Register();
	}
}

That is all there is to it!

Featured

Learn Zend Framework today!

Take an online course and become a ZF2 ninja!

Here is what you will learn:

  • Understand the theory of Zend Framework in details
  • How to implement an enterprise-ready architecture
  • Develop professional applications in Zend Framework
  • Proficiently work with databases in Zend Framework
  • ... and much more!
Zend Framework logo
Author avatar
Bo Andersen

About the Author

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!

Leave a Reply

Your e-mail address will not be published.