Developing software 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

(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:

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)


  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.



  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.



  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/'.

Thoughts? Leave a reply

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