Saturday, April 7, 2012

SEO/Meta Data Helper

Hi everyone,

I am using the latest stable release of Cake - 2.1.1

I am trying to build a helper (or maybe something else) that will
allow me to pull SEO/Meta data from the database, and then make it
available to all my views. I have researched this quite a bit and have
found a few existing plugins/helpers, however they are all too
cumbersome for what I need. I am only interested in the page title,
description, and keywords.

I have created a table in my DB, named "meta", with the following
fields:

id
page
title
description
keywords

I have a corresponding Meta.php model, baked, no changes.

I have a corresponding MetaController.php controller, baked, no
changes.

In very simple terms, what I would like to do is have a helper (or
some other method) fetch the meta data from this table for each page
of my site. I would like to reference each row in the table by the
"page" column, which corresponds to a $page variable I'm using
throughout the site.

In my AppController I have the following:

public $helpers = array('Html', 'Form', 'Seo');

And I have a helper (SeoHelper.php) with the following:

function getTag($page, $tag) {
App::import('Model', 'Meta');
$Model = new Meta();
$meta_data = $Model->find('first', array('conditions' =>
array('Meta.page' => $page)));
$meta_tag = $meta_data['Meta'][$tag];
return $meta_tag;
}

Then, in my layout file (default.ctp), I am using the following for
the title, description, and keywords:

<?php echo $this->Seo->getTag($page, 'title'); ?>
<?php echo $this->Seo->getTag($page, 'description'); ?>
<?php echo $this->Seo->getTag($page, 'keywords'); ?>

Surprisingly, everything is working but I'm worried that the way I've
accomplished this is through trial-and-error, versus truly
understanding what I am doing! Like I said, I am pretty new to Cake.

I would greatly appreciate some help in determining whether this is a
good method to use, or if I should go in another direction. Thanks in
advance for your help and insights.

Justin

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate