Setting a page title in Zend Framework

November 24th 2012

Having a title element on your page is essential. It gives users a quick preview of the page content.

Using ZendFramework you can use this little snippet to control the page title from within a controller action.

PHP Code:
<?php
class IndexController extends Zend_Controller_Action
{
    public function 
init()
    {
        
/* Initialize action controller here */
    
}

    public function 
indexAction()
    {
        
$this->view->headTitle("My Page Title");
    }
}

Alternatively, if you haven't set your view up properly, you can use this:

PHP Code:
<?php
class IndexController extends Zend_Controller_Action
{
    public function 
init()
    {
        
/* Initialize action controller here */
    
}

    public function 
indexAction()
    {
        
$this->_helper->layout()->getView()->headTitle("My Page Title");
    }
}

I hope this helps you out there! A quick definitive way to control your page title from ZendFramework.

Julin commented on Oct 21st 2013

Well, I've used Cake quite often and it's sweeeeeeeeeeeet. It's spmlie to use, fun and fast. Really fast. On spmlie projects, you can't beat Cake's speed. Zend Framework is much more complex and best suited for large, enterprise level projects. For instance, in CakePHP you'll have a hard time building a model that uses a web service as a data source, because all Cake's models are built on top of database tables. Again, if you need to read from more databases, Cake will give you some headaches. But for everyday projects, Cake it's a very good option and should not be cast aside.I started working on a Cake vs. ZF posts Stay tunned!

Roger commented on Nov 16th 2013

@Julin - Cake is a far inferior framework. One that you'll have a hard time finding work knowing. It used to be OK, but now it's a bit rubbish.

Zend is great for projects, and very quick to have something up and running. Personally, I've started to look at Phalcon now.