views:

63

answers:

3

I have set up the RSS Helper (with CakePHP 1.3.4) and all is working - I can access my feeds by /news/feed.rss - as exampled at http://book.cakephp.org/view/1461/Creating-an-RSS-feed-with-the-RssHelper

But I want to be able to do this conditionally, in sudo, something like:

if (!empty($var)) {
    switch ($var) {
        case one :
            $xml = $this->method->find('$var conditions...');
            ... use RSS Helper to serve results as XML.
        case two :
            $xml = $this->method->find('other $var conditions...');
            ... use RSS Helper to serve results as XML.
    }
}

Can I use the RSS Helper in this circumstance? What calls/syntax do I use?

+1  A: 

There are 2 ways to do this.

Basically you can pass a variable like this:

http://yourserver.com/news/feed.rss?recent=20

and then in the controller you can access this variable with

$this->params['url']['recent']; //20

Or you can add a line in your Router file like this:

Router::connect('/feed-:recent/*', array('plugin'=>false, 'controller' => 'news', 'action' => 'feed'), array('recent'=>'[0-9]+'));

This way your url will look like:

http://yourserver.com/news/feed-20.rss

and finally I believe that url like this will work as well:

http://yourserver.com/news/feed.rss/recent:20
Nik
Thank you Nik - so in this case you would still use the RequestHandler component but limit the SQL query from the query string var. I tried something similar to start with but I got error when I added a param after the .rss extention but I used /var syntax instead of ?foo=bar - basic mistake! I put my solution below, but I think I'll try yours which is more simple.
Mark Flint
A: 

The RequestHandler allows you to use a url like /posts/index.rss and it automatically loads the RSS Helper and sends the output to /views/posts/rss/index.ctp, and uses layout at /views/layouts/rss/default.ctp.

But if you want to use the RSS Helper on it's own you need to:

1) include it the controller

var $helpers = array('Rss');

2) in your controller action you need to specify where the output goes, so

$this->render('/posts/rss/index','/rss/default');

In this case I also specified which layout to use in the 2nd argument. The first arg - the index.ctp file location is relative to your views/ directory. The second arg - the layout is relative to your views/layouts/ directory.

So I have a method in my posts_controller that I use to identify which feed is wanted (through a passed var) and subsequently finds the posts and sends them to the correct views and layouts:

function rss(){
    if (!empty($this->params['pass'])){
            $ops=array(
                'conditions'=>'where feedname_id=' . $this->params['pass'][0],
                'order' => 'Post.created DESC',
                'limit' => 10
            );
        $this->set('posts', $this->Post->find('all',$ops));
        $this->render('/posts/rss/index','/rss/default');
    } else {
        $this->redirect(array('controller'=>'feednames','action'=>'index'));
    }
}

There might be better ways to code this - if so please let me know.

Mark Flint
+1  A: 
Abba Bryant