As a followup to my post about SEO-Friendly URLs using Yii, I figured I would post this function I created for displaying SEO-related tags in the layout.

Using the Gii generator with the Yii Framework, a custom base controller is created that is a parent for the others. It already contained a property called pageTitle that was used in some of the views. I decided to keep this location and expand upon it by adding in some more properties related to search engine optimization, and then create a function to display the HTML.

Adding Public Properties (Variables) to your Class

In addition to the title tag and meta description, I’ve added the ability to not index pages, and the Open Graph meta tags used by Facebook. The pageRobotsIndex property defaults to true, and will only be used to display the noindex meta tag if it is specifically set to false. The “OG” or Open Graph meta tags determine the title, descriptive text, and thumbnail that is displayed on Facebook when you share a link or like a page.

I’ve left off keywords because they aren’t really used anymore. Google does not use them, and if you stuff too many keywords in there, Bing will consider it as negative (spammy). I am not bothering with them on new sites.

class Controller extends CController
    /* SEO Vars */
    public $pageTitle = 'Default Title Tag Here | YourWebsiteName';
    public $pageDesc = '';
    public $pageRobotsIndex = true;

    public $pageOgTitle = '';
    public $pageOgDesc = '';
    public $pageOgImage = '';

Create a Helper Function to Display them in your Template

I created the following function in the same Controller class. To be true MVC, I don’t think this really belongs in the controller, because it primarily deals with display. You could create a separate display widget if you want. I can still sleep soundly at night, knowing it’s here because it keeps it simple, it’s in the same place as the variables, I’m not going to have much else in the controller, and just about all the controllers are going to use it.

// Displays SEO-related Variables
public function display_seo()
    // -------------------------
    // Title/Desc
    echo "t".''.PHP_EOL;
    echo "t".''.PHP_EOL;

    // Option for NoIndex
    if ( $this->pageRobotsIndex == false ) {
        echo ''.PHP_EOL;

    // -------------------------
    if ( !empty($this->pageOgTitle) ) {
        echo "t".''.PHP_EOL;
    if ( !empty($this->pageOgDesc) ) {
        echo "t".''.PHP_EOL;
    if ( !empty($this->pageOgImage) ) {
        echo "t".''.PHP_EOL;

Call Function in Template

In the header include for my layout files (views/layouts), this function is then called to display the title tag and meta tags in the portion of the HTML.:

<meta charset="UTF-8">
<?php $this->display_seo(); ?>

Examples: Setting in Your Views

Here is an example of some view pages where values are assigned to these variables:

    // Basic
    $this->pageTitle = 'Your Custom Title Tag';
    $this->pageDesc = 'Your custom description here';

    // Example For a Model View. Appends app name set in config 
    $this->pageTitle = 'Your Title Tag | '.$model->name.' | '.Yii::app()->name;

    // For Facebook Sharing
    $this->pageOgTitle = $this->pageTitle;
    $this->pageOgDesc = $this->pageDesc;
    $this->pageOgImage = '/images/example.jpg';

    // If you don't want the page indexed
    $this->pageTitle = 'Some page to still crawl, but not index';
    $this->pageRobotsIndex = false;

Comments on this Article

  1. Raheel Dharolia says:

    Excellent post buddy… really helpful… Thanks

  2. Josh says:

    Note that I have made an update to Line 24 of “Create a Helper Function” to not CHTML encode the display of the pageOgImage, since this is a URL.

  3. Mitch says:

    Thank you very much. Just what I needed! Your method is a lot simpler then what I had in mind.

  4. Pablo says:

    Thanks a lot, I wasn’t looking for this, but somehow I finished in your blog and it’s really helpful.

  5. David says:

    Love the blog and great idea, it was really helpful.

Comments are closed.