Innovative Computer Solutions,
Development & Technical Support

07975 997026 • enquiries[delete-me]@[delete-me]harpanet.com

User Tools

Site Tools


programming:php:zend-framework:modular-structure-step-3-forms

Zend Framework Modular Structure Step 3 - Forms

Bootstrap

The new ZF autoloader is already configured to look for forms within module folders, we just need to tell it that we are using a modular structure. This is done in the Bootstrap file for each module.

For our Default module, we need therefore to create (or edit if it already exists) the bootstrap file at \wamp\www\square\application\modules\default\Bootstrap.php

The code necessary to implement the Modular structure is:

  class Default_Bootstrap extends Zend_Application_Module_Bootstrap
  {
  }

Note the name of the Module used in the class definition. If this was for a different module, say News, we would use: class News_Bootstrap …etc.

(Thanks to takeshin over at Stack Overflow for this.)

Namespaces

As we are using a Modular layout within ZF, we are no longer able to create forms without specifying their namespaces.

A standard form which we may otherwise create at \wamp\www\square\application\forms\Contact.php would usually assume the Default namespace with the following code:

  class Contact extends Zend_Form
  {
 
    public function init()
    {
      // init form
      $this->setAction('/contact/index')
           ->setMethod('post');
 
      // name
      $name = new Zend_Form_Element_Text('name');
 
      $name->setLabel('Name:')
           ->setOptions( array('size'=>'35') )
           ->setRequired(true)
           ->addValidator('NotEmpty', true)
           ->addValidator('Alpha', true)
           ->addFilter('HtmlEntities')
           ->addFilter('StringTrim');
 
      // email
      $email = new Zend_Form_Element_Text('email');
 
      $email->setLabel('Email address:');
      $email->setOptions( array('size'=>'50') )
            ->setRequired(true)
            ->addValidator('NotEmpty', true)
            ->addValidator('EmailAddress', true)
            ->addFilter('HtmlEntities')
            ->addFilter('StringToLower')
            ->addFilter('StringTrim');
    }
  }

…but now must be created at \wamp\www\square\application\modules\default\forms\Contact.php and must explicitly reference the full namespace:

  class Default_Form_Contact extends Zend_Form
  {
 
    public function init()
    {
      // init form
      $this->setAction('/contact/index')
           ->setMethod('post');
 
      // name
      $name = new Zend_Form_Element_Text('password');
 
      ...etc...
 
    }
  }

… and the associated form controller at \wamp\www\square\application\modules\default\controllers\ContactController.php must also instantiate the form using the full namespace in its indexAction:

  class ContactController extends Zend_Controller_Action
  {
 
    public function init()
    {
      $this->view->doctype('XHTML1_STRICT');
    }
 
    public function indexAction()
    {
      $form = new Default_Form_Contact();
      $this->view->form = $form;
 
      if ($form->getRequest()->isPost())
      {
        ...etc...
      }
    }
  }
 

As far as I am aware so far, that ties down all the elements necessary to make a proper modular structure work in Zend Framework. As I said originally, Zend doesn't exactly fall easily into the modular structure without a bit of a fight.

I'll add more as my progress continues if I find additional elements that require tweaking.

programming/php/zend-framework/modular-structure-step-3-forms.txt · Last modified: 15/06/2013 16:30 (external edit)