Richard Parnaby-King

Web Developer – PHP, Zend Framework and Actionscript 3

Posted on | | 2 Comments

Meta keywords and description used to be used by search engines to help work out what your website is about. Nowadays they are used to web applications to work out how to describe a specific page (for example, paste a link into Google+ and the meta description of the page will be fetched and shown). If you are writing a CMS that allows the user to populate the meta keywords and description of a page, but the user does not fill these fields, then it is nice to have a default value.

This post will share a custom View Helper that will check for your application adding empty meta details, and if there are, populating the values with default values from the application config.ini file.

First, we need to do add a folder structure for our view helpers. Create the following tree structure:

root
|->application
|->library
   |->Zend
   |->RPK
      |->View
         |->Helper

Now, we need to tell our application where to find our view helper. In the application bootstrap.php file add the following function:

<?php
//Application/Bootstrap.php
protected function _initViewhelper()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addHelperPath('RPK/View/Helper', 'RPK_View_Helper');
}

This allows the application to access our view helper.

Here is the code for the view helper. We extend the already existing Zend view helper ‘HeadMeta’. Feel free to download this file

<?php
/**
 * If adding meta keywords or description to a page, and the values are empty, then populate them with default meta details from config file
 *
 * Add default meta keyword and description to a page
 *
 * @copyright		2012 Richard Parnaby-King
 */

class RPK_View_Helper_HeadMeta extends Zend_View_Helper_HeadMeta
{

  /**
   * If value name = keywords or description, check if content is null. If it is, then populate with default
   * meta details.
   *
   * @param  string $value
   * @return void
   * @throws Zend_View_Exception
   */
  public function set($value)
  {
    //If attempting to add a keyword or description, and the value is empty or null, the load the config file, get the relavent option and set the value
    if ($value->type == 'name' AND ($value->content == null OR strlen($value->content) === 0) AND ($value->name == 'keywords' OR $value->name == 'description'))
    {
      //get options
      $config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
      switch ($value->name)
      {
        case 'keywords':
          $setting = $config['default']['meta']['keywords'];
          break;
        case 'description':
          $setting = $config['default']['meta']['description'];
          break;
      }
	  //set the value
      if ($setting)
      {
        $value->content = $setting;
      }
    }

    if (!$this->_isValid($value))
    {
      $e = new Zend_View_Exception('Invalid value passed to append; please use appendMeta()');
      $e->setView($this->view);
      throw $e;
    }

    return $this->getContainer()->append($value);
  }

Usage

<?php
//layout script
echo $this->headMeta();
<?php
//View script (eg page.phtml)
//$this->page = page object passed to view script from controller
$this->headMeta()
    ->setName('keywords', $this->page->metaKeywords)
    ->setName('description', $this->page->metaDesc);

As you can see there is no special instantiation code needed.

Posted By:

Comments

  • http://www.chinasingels.com/Manuela37 http://www.chinasingels.com/Manuela37

    Wow that was odd. I just wrote an extremely long comment but after I clicked
    submit my comment didn’t appear. Grrrr… well I’m not writing all that over again.
    Anyways, just wanted to say wonderful blog!

    • http://richard.parnaby-king.co.uk/ Richard Parnaby-King

      Hi Manuela,

      I’m sorry you lost your original comment. I have checked my logs and cannot find any trace of it. I am afraid it is lost forever.

      Thanks for the comment ;)

      Rich.

  • ABOUT

    Having five years of programming experience in PHP, Zend Framework and ActionScript3, I have a very strong working knowledge of object orientated programming.

    I am a PC Gamer! Playing FPS, RTS, RPG and the occasional MMO since 1996 I have a huge number of a variety of fast-paced games.

  • Recent Posts

  • Categories

  • RSS SUBSCRIBE TO OUR FEED

  •