Adding SEO functionality
Published on Nov 15, 2009 by Jamie Munro
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.
- 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:
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:
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:
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:
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.