Free Android CakePHP GMaps Articles by Bali Web Design

July 7, 2009

Create RSS using cakephp

Filed under: php — Tags: — admin @ 2:21 am

It is easy to add RSS into your website using cakephp. Cakephp has a built in library to code or create a RSS output to your visitor. For you that doesn’t know yet RSS, it is a web 3.0 capability to communicate between two website about new update on one website. Its stand for Really Simple Syndication. The purpose isĀ  to give a standard output about new change on your website. For example a blog can have RSS to publish their latest post or article.

i will explain a method to add RSS capability into your cakephp website. In this example we have posts controller for our blog post and we want to create RSS for post table. The rss url will look like this

http://www.yoursite.com/posts/rss or change routing for http://www.yoursite.com/rss into posts controller and rss action.

The implementation will explain in 4 step, so here we are

  1. open app/config/routes.php, add this line and then save
    Router::connect(‘/rss’, array(‘controller’ => ‘posts’, ‘action’ => ‘rss’));
  2. open app/controller/posts_controller.php and create new method or action
    function rss(){
    $this->set(‘posts’, $this->paginate(‘Post’));
    }
  3. create app/views/posts/rss.ctp, and paste this lines
    <?php
    echo $rss->items($posts, ‘transformRSS’);
    function transformRSS($post) {
    return array(
    ‘title’ => $post[‘Post’][‘title’],
    ‘link’ => array(‘action’ => ‘show’, $post[‘Post’][‘id’]),
    ‘guid’ => array(‘action’ => ‘show’, $post[‘Post’][‘id’]),
    ‘description’ => $post[‘Post’][‘text’],
    ‘author’ => $post[‘User’][‘username’],
    ‘pubDate’ => $post[‘Post’][‘created’]
    );
    }
    ?>
  4. at this point your RSS is ready. check by open URL http://www.yoursite.com/posts/rss or http://www.yoursite.com/rss

As i said, adding or create rss using cakephp is relatively simple to do.


No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment