How to setup a route in CakePHP

H

If you are like me and are creating personal web sites, one of the best ways to gain traffic to your site is through SEO.  I won’t go into detail here as I want to focus on how to setup a route in CakePHP, but having SEO friendly URLs will go a long way in helping.

First let’s define what a “route” is.  In CakePHP it allows you to add a different path to access a specific controller and/or function in that controller.

For example, let’s say I want to create a CMS.  I might create a controller called “StaticContent” and then place a function for each page a client would like to create.  I’m pretty sure I would start getting a lot of calls saying, “Jamie, why is my URL http://www.myaddress.com/StaticContent/myCustomPage?”  So to avoid those calls, we can use a route to make it more accessible and SEO friendly.

To create our route it’s quite simple, open up app/config/routes.php in your editor of choice.  To create a route for our static content, we would create something like this (you will need to update the names accordingly for your purpose):

Router::connect(‘/seo-friendly-url’, array(‘controller’ => ‘StaticContent’, ‘action’ => ‘unSeoFriendlyURL’));

The syntax above is saying, if we find the URL “/seo-friendly-url”, CakePHP should look for the content in the StaticContent controller and our unSeoFriendlyURL function.

The above example is a great way of how you can quickly create SEO friendly URLs and continue to create controllers and functions as you normally would.

Hopefully you have enjoyed today’s article, I am going to try and continue to create some more short and useful CakePHP examples.

About the author

By Jamie

My Books