A fusion of technology, music, and geekyness.

CakePHP SEO Plugin

I just wanted to quickly announce our new SEO Plugin for CakePHP. It provides an easy way to add SEO friendly URLs, page titles, and META fields to your CakePHP applications.

Installation

1. Add the MySQL table.


CREATE TABLE IF NOT EXISTS `seo_seos` (
  `id` int(11) NOT NULL auto_increment,
  `related_model_id` int(11) NOT NULL,
  `related_model_name` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  `keywords` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `related_model_id` (`related_model_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

This table will hold the SEO metadata for each model record.

2. Download the code and drop it in your /plugins directory. The code is readily available at GitHub.

3. Add the fields to your forms

	<fieldset>
		<legend>SEO</legend>
	<?php
		if ($this->action == 'admin_edit') {
			echo $form->input('Seo.id');
		}
		echo $form->input('Seo.slug');
		echo $form->input('Seo.title');
		echo $form->input('Seo.description');
		echo $form->input('Seo.keywords');
	?>
	</fieldset>

4. Add the preparation code to beforeRender() in AppController.

	function beforeRender() {
		$this->SearchEngineOptimizer->prepare();
	}

5. Add Seo.Seo to your $actsAs array in desired models or AppModel. Warning: Seo.Seo MUST come BEFORE Containable.

var $actsAs = array('Seo.Seo', 'Containable');

6. Add Seo.SearchEngineOptimizer to your $components array in AppController.

var $components = array('Auth', 'Seo.SearchEngineOptimizer');

7. Add $seo_for_layout to your /views/layouts/default.ctp (or whichever layout files you are using). It should look something like the following.

	<title>
		<?php echo $title_for_layout; ?> :: <?php echo Configure::read('website.general.name') . ' :: ' . Configure::read('Application.name'); ?>
	</title>
	<?php echo $seo_for_layout; ?>

8. Finally, add routes to make your URLs extra sexy. For example:

Router::connect('/recipes/*', array('controller' => 'recipes', 'action' => 'view'));

This should allow you to hit: http://mydomain.example/recipes/my-recipe-slug

Enjoy! Please let me know what I did wrong and what I did right. Suggestions and critiques are always welcome!

One Response to “CakePHP SEO Plugin”

  1. Dustin says:

    Thanks! I look forward to testing it out!

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes