JavaScript, jQuery, HTML5, CSS & More

Archives

Meta

Search Engine Friendly URLs in CakePHP

One thing that is not built-in to CakePHP is the ability to use search engine friendly controller and action names when those names consist of more than one word. CakePHP does an excellent job of allowing friendly URLs in general, but this typically involves single-word controller and action names. So what about controller and action names with multiple words? In CakePHP, this is handled with class names defined in CamelCase and method names defined in camelBack.

For instance, you may have a blog system built with CakePHP that uses a typical controller name, such as PostsController, and a typical action name, such as view, which takes a blog post slug (search engine friendly name) as its first argument. In the MVC paradigm, this would be accessed with a URL like the one below.

http://myblog.com/posts/view/search-engine-friendly-url

http://myblog.com/posts/view/search-engine-friendly-url

Most everyone would agree that this is a “friendly” URL, and in this case, the sluggable behavior, or something similar, would allow you to easily provide a slug as the first argument to the PostsController->view() method, as shown below.

The purpose of using a slug is to provide a dash-separated (hyphen-separated) version of the blog post title in the URL for SEO, a common feature of most popular blog publishing systems.

Multiple Word Controller Names

CakePHP allows multiple-word controller names to be accessed from the URL in several ways.

CamelCase ControllerName, as in “http://myblog.com/ControllerName”;

camelBack controllerName, as in “http://myblog.com/controllerName”;

underscore-separated, as in “http://myblog.com/controller_name”;

or any combination of the above.

This gives you a lot of options for selecting your URL scheme of choice, but one option that is not available out of the box with CakePHP is a dash-separated controller name in the URL. This troubles me because I prefer dash-separated URLs, and Google recommends hyphens instead of underscores in your URLs.

I also prefer to avoid uppercase letters in URLs entirely, so with CakePHP, the only viable option out of the box for multiple word controller and action names is to use underscore-separated names. For these reasons, I decided to implement my own way of allowing for dash-separated controller and action names in the URL to map to their respective CamelCase and camelBack names in CakePHP.

I will not claim that my method for implementing dash-separated controller and action names in CakePHP is the best way, and it does have its downside, but it is the quickest way that I could think of to accomplish this task without too much trouble.

To do this, I simply added the following snippet of code to the /app/config/routes.php file.

In this block of code, the default $_GET['url'] parameter that gets passed from the query string to CakePHP is analyzed for the existence of dashes (hyphens) in either the controller or action portion of the URL. If either the controller or action name contains any dashes, those dashes are replaced with underscores, and in the case of the action name, it is converted to its camelBack name using CakePHP’s Inflector::variable method. This only applies if you are using action names defined in camelBack, of course.

The Downside

The downside to using this method is that it prevents the proper use of CakePHP’s built-in reverse routing. In other words, using the HtmlHelper::link method to create a link with a code block like the one below will not map to a dash-separated version of the URL in the generated link.

Improving Reverse Routing in CakePHP in the Future

I would recommend that the CakePHP team look at adding a configuration option in the future that would allow you to define the type of URLs you would like to be generated when using reverse routing. This way you could always have clean URLs in a consistent format by simply using reverse routing, the /app/config/routes.php configuration file, and a URL type or style setting.

For example, a method could be called inside routes.php with an array of parameters to set the style for all generated URLs:

@Saliem: The .htaccess file is already set up to point all URLs to the CakePHP index file and route them from there. Handling the routing in the framework itself allows for cleaner and more centralized business logic, but of course everything is debatable.

Essentially, I dont’ want ‘article’ & ‘view.’ How could this be implemented. I’m thinking that ‘rel canonical’ will not work because the ‘home’ of the URL is unique and not duplicated. Thus I’m thinking that mod_rewrite in Apache is the only option, but have no clue how this works with CakePHP. Many, many thanks in advance for your help!