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-2-layouts

Zend Framework Modular Structure Step 2 - Layouts

Although it is not created by the zf command line tool, the default location for Zend Framework Layout files is in \wamp\www\PROJECT NAME\application\layouts (ie. \wamp\www\square\application\layouts) which is clearly not within our required Modular structure.

I'd prefer it to be located in \wamp\www\square\application\modules\default\layouts but apparently this isn't how ZF would like it to work.

Instead, ZF is set up to locate modular based layout files in …\application\modules\[module name]\views\scripts\layout.phtml

Making Layout files Modular

I don't know enough to understand what this is doing yet, but to enable the ZendLayout module to understand the MVC structure, we need to call the startMVC() action within our application Bootstrap file. Therefore…

Edit \wamp\www\square\application\Bootstrap.php Place the Zend_Layout::startMvc(); inside the _initLayout() action…

  protected function _initLayout() {
    Zend_Layout::startMvc();
  }

That is, thankfully, all we need to do in order to enable Zend Framework module based layout files.

Additional Modular Layout Files

The limitation of this ZF approach should be immediately obvious: what if we want more than one layout within our module?

Thankfully, ZF allows us to set which layout file to use from within the init() Action within our Controllers. If we don't specify a layout to use, ZF will use the default one named layout.phtml (ie. \wamp\www\square\application\modules\default\views\scripts\layout.phtml )

For example, in the square example application, we have a 'static content controller' located at \wamp\www\square\application\modules\default\controllers\StaticContentController.php which displays static content (eg. about us, contact us, etc).

Within the init() action of this controller, we specify which layout file we would like to use. Below is the entire controller code as I currently have it:

  <?php
 
    class StaticContentController extends Zend_Controller_Action {
 
    public function init() {
      // use different layout script with this action:
      $this->_helper->layout->setLayout('foobaz');
    }
 
    // display static views
    public function displayAction() {
 
      $page = $this->getRequest()->getParam('page');
 
      $file = $this->view->getScriptPath(null) ."/". $this->getRequest()->getControllerName() ."/$page.". $this->viewSuffix;
 
 
      if ( file_exists( $file ) ) {
 
        $this->render($page);
 
      } else {
 
        throw new Zend_Controller_Action_Exception('Page not found matey: '.$file, 404);
 
      }
    }
  }

Just for testing purposes, you can see I have told ZF to display our static content using the foobaz layout template. This layout is located at \wamp\www\square\application\modules\default\views\scripts\foobaz.phtml

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