Set page title in CakePHP

I just noticed that several people are searching for a phrases like "cakephp home change page title" or "page title cakephp" and landing up here. I am sure they must be returning disappointed - so this is an quick tip for them

Update:For CakePHP 1.3 the syntax has changed and it is now

PHP:
  1. $this->set("title_for_layout","Page Title Value");

Rest of the post is deprecated

To set a page title in CakePHP in your controller write

PHP:
  1. $this->set('title', 'My Page Title');

or the less preferred and probably soon to be deprecated

PHP:
  1. $this->pageTitle = 'My Page Title'

However if you are using the pages controller to render a few static pages and want custom title, in your view (static page) use

PHP:
  1. $this->pageTitle = 'My page title';

Hope that helped

About Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

9 Responses to Set page title in CakePHP

  1. Ian March 12, 2008 at 5:29 am #

    I'm not quite sure that the second one will be deprecated. At least not in full.

    I often use the format at the top of my controller:
    var $pageTitle = 'My Page Title';

    This is quite useful for setting a global page title. It's by far the easiest way to do it globally that I'm aware of :)

    • meizitang February 23, 2011 at 10:06 pm #

      Thanks. I don't know why they make this change.

  2. julien June 23, 2008 at 4:04 am #

    yes that helped!

  3. lakfew August 21, 2008 at 10:18 pm #

    Splendid! Thanks a lot for responding to your visitors' habits.

  4. Ryan October 28, 2008 at 11:46 pm #

    Nice quick answer! I like how you molded the page to what people were searching for. I could learn something from this.

  5. Paamayim January 21, 2009 at 3:55 am #

    Hey the third code block is the same as the second one.

    • Tarique Sani January 21, 2009 at 9:46 am #

      Yes, of course it has to be the same - please read it in context of what is written :)

  6. Magnus August 6, 2010 at 9:31 pm #

    This has now been changed to $this->set("title_for_layout", "My page title");

  7. igi ladera April 14, 2011 at 9:08 am #

    Thanks for this wonderful and very helpful information.

Leave a Reply