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:
[php]
root
|->application
|->library
   |->Zend
   |->RPK
      |->View
         |->Helper
[/php]

Now, we need to tell our application where to find our view helper. In the application bootstrap.php file add the following function:
[php]
bootstrap(‘view’);
$view = $this->getResource(‘view’);
$view->addHelperPath(‘RPK/View/Helper’, ‘RPK_View_Helper’);
}
[/php]

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]
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);
}
[/php]

Usage

[php]
headMeta();
[/php]

[php]
page = page object passed to view script from controller
$this->headMeta()
->setName(‘keywords’, $this->page->metaKeywords)
->setName(‘description’, $this->page->metaDesc);
[/php]

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

Posted By:

Comments

  • ABOUT

    Having fifteen 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