Zend Framework – how to autoload forms within a module 6

I got to thinking that it didn’t make sense to have a common /forms directory that would share forms between two modules when the forms would not be shared. A /forms directory within the specific module directory seemed more appropriate. The documentation on this isn’t very clear, but after some digging and trial and error I figured it out.

First, the directory structure.  Notice the location of the forms directories.











First, add the two below lines to your application.ini file.

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

Second, create a boostrap file for each module.  The file, named Bootstrap.php, should be placed in the root of the module directory and the class name should be {module name}_Boostrap. This bootstrap file will cause zend framework to automatically add the new forms directory to the autoloader.

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}

Third, add a form class to the /forms directory. A login form would have a filename of Login.php and a class name of {module name}_Form_Login.

class Admin_Form_Login extends Zend_Form

Lastly, call your form from a controller file from within the same module.

$form = new Admin_Form_Login();

Be careful if you’re adding a Bootstrap.php file to the default module! From Zend:

The reason that the default module is skipped is because the documented use case – particularly how we setup an application in Zend_Tool – is that the default module is directly beneath application/ and contains the application bootstrap. If we then allow loading the default module’s bootstrap… we get into a recursive loop.

Leave a comment

Your email address will not be published. Required fields are marked *

6 thoughts on “Zend Framework – how to autoload forms within a module