Feb 05 2011

Setting a default page title in Zend Framework and modifying per action

There may be a better way of doing this, I'm open to suggestions. Still, my goal was to have a default page title, manipulating when required in an action.

I initially tried to just use $this->headTitle('foobar'); in my layout and prepend in the controller action. This didn't work out too well, due to the order in which they're executed - append and prepend are reversed. Not too intuitive.

Therefore, I created an _init method in the Bootstrap class:

public function _initTitle()
    $view = $this->bootstrap('view')->getResource('view'); 

Modifying the title in the action is very simple:

public function fooAction()
    $this->view->headTitle()->prepend('foo | ');

Accessing the foo action will yield:

<title>foo | foobar</title>

For reference example was written with Zend Framework 1.11.2.

comments powered by Disqus


I'm Ade Slade, a PHP web developer.