Adding SEO functionality Adding SEO functionality

Two of the biggest things that will improve your search engine rankings are keyword rich website titles and keyword rich links to your content. Today’s lessons will cover both of these topics.

Objectives

  • Improve our search engine rankings
  • Set a title tag
  • Map a specific URL to a custom controller and action
  • Avoid using ids or numbers in our URL

Before I start, I would like to make it clear that this article is and does not intend to be a be-all-to-end-all of the SEO necessities for your website. It is meant to describe some excellent techniques to quickly and easily improve upon CakePHP to make it more SEO friendly.


In this article we will focus on three techniques to easily adopt CakePHP and make it much more SEO friendly.

The three ways are:

  • Use of title tags
  • Use of routes
  • Use of intelligent view() functions

Use of title tags

Let’s begin with title tags. By default, if you do not set a title tag, CakePHP uses the name of your function. You will quickly find that this is not useful as Google frowns upon non-unique titles. Before long Google will have multiple title tags called Index and Add, etc… for your content. This will not get you too far.

CakePHP offers a simple solution to control this, you need to set a class variable called $pageTitle.

For example, inside anyone of our controllers we could do the following:

  1. $this->pageTitle = ‘This is our new title for this page!’;

Obviously, this won’t get you ranked very high either, but it may be better than Index!

Use of routes

Our next route is to use CakePHP routes, pun intended. Inside of the app\config directory there is a file called routes.php. Below is a default example of one:

  1. <?php
  2. /* SVN FILE: $Id: routes.php 7945 2008-12-19 02:16:01Z gwoo $ */
  3. /**
  4. Short description for file.
  5. *
  6. In this file, you set up routes to your controllers and their actions.
  7. Routes are very important mechanism that allows you to freely connect
  8. different urls to chosen controllers and their actions (functions).
  9. *
  10. PHP versions 4 and 5
  11. *
  12. CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  13. Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  14. *
  15. Licensed under The MIT License
  16. Redistributions of files must retain the above copyright notice.
  17. *
  18. @filesource
  19. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  20. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21. * @package cake
  22. * @subpackage cake.app.config
  23. * @since CakePHP(tm) v 0.2.9
  24. * @version $Revision: 7945 $
  25. * @modifiedby $LastChangedBy: gwoo $
  26. @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
  27. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  28. */
  29. /**
  30. Here, we are connecting '/' (base path) to controller called 'Pages',
  31. its action called 'display', and we pass a param to select the view file
  32. to use (in this case, /app/views/pages/home.ctp)...
  33. */
  34. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  35. /**
  36. ...and connect the rest of 'Pages' controller's urls.
  37. */
  38. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  39. ?>

Ignoring all of the comments, the above file tells CakePHP that, when we arrive at “/”, e.g. in our test project http://localhost/HelloWorld, that it should load the display() function in the pages_controller.php and pass in the variable “home”. That means that it will load the home.ctp file inside our app\views\pages folder. If we don’t create our own file with that name, CakePHP will load its default home.ctp file inside of cake\libs\view\pages.

The next action in the routes.php file tells CakePHP that any request to “/pages/*” should load this same display() function; however, instead of loading the home.ctp view, it will load whatever name we pass in. For example, if we visited http://localhost/HelloWorld/pages/test our CakePHP function would attempt to load app\views\pages\test.ctp and display it in our layout.

Now, how can we use routes to our advantage? Let’s say we were to use CakePHP’s bakery to create a user registration page. By default, we would access it as follows: http://localhost/HelloWorld/users/add. This is not the most user friendly URL in the world. Instead we want it to be: http://localhost/HelloWorld/register. To accomplish this, we would add the following line to our routes.php file:

  1. Router::connect(‘/register/*’, array(‘controller’ => ‘users’, ‘action’ => ‘add’));

Use of intelligent view() functions

Our last example is to make intelligent view() functions. You may be wondering what I mean by an intelligent view function. Well, by default, when we use CakePHP’s bakery all of our view functions load elements using id’s. A URL like this: http://localhost/HelloWorld/categories/view/3 is not very search engine friendly. However, what if we could change this URL to: http://localhost/HelloWorld/categories/view/search-engine-optimization? This would certainly make it a lot better looking to search engines.

To accomplish this, we need to update our view function to have something similar to the following:

  1. function view($name = "") {
  2. // get the categories name and information
  3. if (intval($name) == 0 && strlen($name) == strlen(intval($name))) {
  4. $id = intval($name);
  5. $category = $this->Category->find(‘first’, array(‘conditions’ => array(‘id’ => $id)));
  6. } else {
  7. $category = $this->Category->find(‘first’, array(‘conditions’ => array(‘name’ => $name)));
  8. }
  9. }

In the above code, we check if name is an integer. If name is an integer, we will perform a regular search by id to get the record. However, if the name is actually a string, we will search for the category by name to find it. Technically we don’t need to concern ourselves if an integer is passed into the function, but the first time I created a procedure like this, I already had indexed links with the id in the URL. Not wanting to lose these rankings, I made the function intelligent to handle both.

As I mentioned at the beginning of this chapter, these three examples will considerably help your SEO rankings; however, don’t stop there. If you are interested in SEO, perform some more research and see how you may utilize CakePHP to your advantage to quickly implement system-wide SEO changes.

Published on Nov 15, 2009

Tags: SEO (Search Engine Optimization) | CakePHP Tutorial | seo

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.