Thursday, November 12, 2009

CakePHP - Make Your URL SEO Friendly Using Routes

Routing is a feature that maps URLs to controller actions.


URL pattern default routes:
    http://example.com/controller/action/param1/param2/param3

In our example:
http://caketest.local/posts/add   /* here 'posts' is the controller and 'add' is the action.
It will map to: PostsContoller -> add();

http://caketest.local/posts/edit/2 /* here 'posts' is the controller, 'add' is the action, and '2' is the param1
It will map to: PostsController->edit(2);

Routes are defined in '/app/config/routes.php' file using the Router::connect() method.

Basic format:

Router::connect(
    'URL',
    array('paramName' => 'defaultValue'),
    array('paramName' => 'matchingRegex')
)

Now, copy-paste following code in that file:


Router::connect(
'/posts/:slug-:id',
array('controller' => 'posts', 'action' => 'view'),
array(
// order matters
'pass' => array('slug' , 'id'),
'id' => '[0-9]+'
)
);

'/posts/:slug-:id'  is the default URL patter. (Here 'posts' can be replaced by any string, 'slug' is a unique identifier used as url part, and 'id' is the article id).
The next array() will match this URL pattern with $PostsController->view() method.
The array key 'pass' will pass two parameters 'slug' and 'id', the next line sets up rules for acceptable values for 'id'.
That's it.

Now in your index.ctp file, (found under '/app/views/posts/index.ctp')

Replace these lines:
echo $html->link($post['Post']['title'],
"/posts/view/".$post['Post']['id']);

With:
echo $html->link($post['Post']['title'], array(
'controller' => 'posts',
'action' => 'view',
'slug' => Inflector::slug($post['Post']['title'],'-'),
'id' => $post['Post']['id'] )
);

Note: we have used Inflector::slug method to generate the SEO friendly url part.
And you are done. 

To view a particular post,
Add this method at PostsController (File '/app/controllers/posts_controller.php)

function view($id = null, $slug = null) {
$this->Post->id = $this->params['id'];
$this->set('post', $this->Post->read());
}

Now create a new view file: ('/app/views/posts/view.ctp')
<!-- File: /app/views/posts/view.ctp -->
<h1><?php echo $post['Post']['title']?></h1>
<p><small>Created: <?php echo $post['Post']['created']?></small></p>
<p><?php echo $post['Post']['body']?></p>

Now point your browser to:
http://caketest.local/posts/

You can see the list of posts. Note the titles have been made SEO friendly. (Like: http://caketest.local/posts/Second-Blog-Post-Title-2 ). You can read the post by following the post title.

That's it.
Since CakePHP routing has a lot more to do, you MUST read this article thoroughly to understand how it works. This is very important to fine tune your application.

So, I think we have learnt how to include 'ADD', 'UPDATE', 'DELETE', and 'VIEW' operations in a database, plus, how to make SEO friendly urls.

In my next post, I'll tell you how to develop a workable application within minutes using 'scaffolding'.

4 comments:

Marcus_Dane said...

When I was still a beginner as an SEO looking for SEO friendly URLs, I never thought that no-follow links have no effect in spreading my keywords to the SEO friendly URL that I've found. This serves as a great lesson to the upcoming employees to our SEO web design company.

Siddhartha said...

Thanks.

MAZ said...

Thanks, this will be helpful for my new website

Unknown said...

Very helpful. Thanks ;)