Pragmatism in the real world

Where to set up your view?

With Zend_Layout on the horizon, I’ve been looking at how to use it and it will remove the need for my own front controller plugin which I call SiteTemplate. As a result, I need to find a new home for the view customisation stuff I do in SiteTemplate.

My current plan is to create a new front controller plugin called ViewSetup (for want of a better name!) that contains just the view setup stuff from

<?php
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Front Controller plug in to set up the view with the Akra view helper
 * path and some useful request variables.
 *
 */
class Akra_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract
{    
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->setView(new Akra_View());

        // add helper path to View/Helper directory within this library
        $prefix = 'Akra_View_Helper';
        $dir = dirname(__FILE__) . '/../../View/Helper';
        $viewRenderer->view->addHelperPath($dir, $prefix);
    
        // set up variables that the view may want to know    
        $viewRenderer->view->baseUrl = $request->getBaseUrl();
        $viewRenderer->view->module = $request->getModuleName();
        $viewRenderer->view->controller = $request->getControllerName();
        $viewRenderer->view->action = $request->getActionName();
        $viewRenderer->view->theme = $request->getParam('theme', 'default');
    }
}

(I’ll talk about how cool Akra_View is another time…)

I had a dig into Zend_Layout’s source and it collects its view object from the view renderer too, so this works as expected. (*phew*!)

The alternative, as I see it is to put the view creation into my bootstrap and then create a series of view helpers like this one for the base url:

<?php

class Places_View_Helper_BaseUrl
{
    protected $_baseUrl;
    
    function __construct()
    {
        $fc = Zend_Controller_Front::getInstance();
        $request = $fc->getRequest();
        $this->_baseUrl =  $request->getBaseUrl();
    }
    
    function baseUrl()
    {
        return $this->_baseUrl;
    }
}

How does everyone else do it? Is the bootstrap + many view helpers approach a better one than using a front controller plugin? Have I missed a completely obvious better solution?

7 thoughts on “Where to set up your view?

  1. $viewRenderer->view->theme = $request->getParam('theme', 'default');

    Hi, can you explain how this "theme" thing works ? Thanks

  2. I've personally been setting most of my view in the bootstrap. Now I think I'm going to switch to your plugin method, it looks much nicer. The view helper method seems like it would take a lot of extra work for each variable. (and possibly a lot of overhead too)

    Erik

  3. Kaloyan,

    It's nothing clever :) We just put all the js, img and css files into a 'theme' subfolder with www/. At the moment we have two: 'default' for the front end and 'admin' for the administration system.

    It's mainly intended as an organisational thing so we don't get confused as to which CSS files go with which background images.

    Regards,

    Rob…

  4. Guess I need to refactor my method…

    I setup the view for the most part during boot strap… the menu's are added to the layout using a controller plugin, and the minor stuff like links I use a helper for… something along the lines of link('module/controller/action') ?>

  5. Many thanks for sharing your plan, this kind of tips and information are very much needed to clear some of the newbies confusion out there (pointing to self).

    The way I was doing it is by creating an extended Zend_Controller_Action calling it e.g. BaseController, then all Action Controllers would inherit from it. This way I can do any initialization I'd like in BaseController's init().

    I'm not sure which approach would be better. But Plugin is certainly more re-usable. Any other pros / cons?

  6. Amr,

    To be honest, I don't think it really matters if you use a plugin or a parent controller class.

    The best thing about action helpers is that you can limit the helper to doing one thing only, whereas if you have a parent, then it will end up doing lots of little things.

    Regards,

    Rob…

  7. I had problems using $request->getBaseUrl() when linking to CSS or image files.
    Instead I found it to be safer to use $request->getBasePath() to make sure a trailing 'index.php' get's dropped in all circumstances.

    When using getBaseUrl() I sometimes ended up with '/foo/index.php/main.css'. This would happen if the index.php would be called directly.

    When using getBasePath() I would always get '/foo/'.

Comments are closed.