Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create RSS (Rich Site Summary) in Cakephp framework

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 293
    Comment on it

    This tutorial help us in creating RSS Feed in cakephp framework. For more on RSS Feed see the link below:

    "http://findnerd.com/account#url=/list/publish/What-is-RSS-Rich-Site-Summary-/2965/"
    

    Let use take a table users in the database.

    Step 1: We make changes in our routes.php file

    path: projectfile/app/core/routes.php

    <?php
    
    Router::parseExtensions('rss');
    
    /**
     * Load the CakePHP default routes. Only remove this if you do not want to use
     * the built-in default routes.
     */
    require CAKE . 'Config' . DS . 'routes.php';
    
    ?>
    

    Step 2: Now we will add a 'RequestHandler' and 'Paginator' component in the UsersController.php file

    path: projectfile/app/Controller/UsersController.php

    <?php
    
    class UsersController extends AppController {
    
        var    $uses          = array('User');
        public $components   = array( 'Paginator','RequestHandler');
        public $helpers            = array('Html');
    
    public function index(){    
    
            $this->User->recursive = 0;
            $this->set('users',$this->paginate());
        }
    }
    
    ?>
    

    Step 3: Now we will create rss layout as we create a layout for your view file.

    Path: projectfile/app/View/Layout/rss/default.ctp

    <?php
        echo $this->rss->header();
        $channel = $this->rss->channel(array(),$ChannelData,$content_for_layout);
        echo $this->rss->document(array(),$channel);
    ?>
    

    channel() => it is rss helper method.

    Header() => it is rss helper method.

    Document()=> it is rss document method. It takes 2 parameters one is empty array and another is the contents of the rss


    Step 4: Now we will create rss view file and this view file will be created for the index action of in UserController.php file.

    Path: projectfile/app/View/User/rss/index.ctp

    <?php
    
    $channelData=array(
    'title'=>'Our Current Users',
    'link'=>$this->html->url(array(
    'controller'=>'users',
    'action'=>'index',
    'ext'=>'rss',
    )),
    'description'=>'List of the users registered with the app',
    'language'=>'eng-us'
    );
    
    $this->set('ChannelData',$channelData);
    
    foreach($users as $user){
    
        echo $this->rss->item(
            array(),
            array(
            'Title'=>$user['User']['user_name'],
            'link'=>array(
                'controller'=>'users',
                'action'=>'view', $user['User']['id'],
            ),
            'guid'=>array(
                'controller'=>'users',
                'action'=>'view', $user['User']['id'],
            ),
            'description'=>"<div>".strip_tags($user['User']['email'])."</div>",
            'registrationDate'=>$user['User']['created'],
            )
        );
    }    
    ?>
    

    Step 5: Now to see the RSS feed use the following url:

    url: yourdomain/projectfilename/controller/index.rss
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: