Andrew
Andrew

Reputation: 13

How to config routes in cakephp2 to index.rss

I want to write new route for RSS-View. The call /news/index.rss must open the RSS-Feed, but it open wrong methode.

routes.php

Router::parseExtensions('rss');
...
// don't work
Router::connect('/news/index.rss', array('controller' => 'news', 'action' => 'index'));
...
// open News:indexForPage()
Router::connect('/news/indexForPage/*', array('controller' => 'news', 'action' => 'indexForPage'));
...
// List width pagignation (News:index())
Router::connect('/news/*/:slug/:page',
        array(
                'controller' => 'news',
                'action' => 'index'
        ),
        array(
                'competence_id'=>'[a-z,0-9,A-Z,\-]+',
                'page'=>'[a-z,0-9,A-Z,\-]+'
        )
);
...
// After call '/news/{Title-of-the-Article}-{ID}', it open News:view(ID)
Router::connect('/news/**', array('controller' => 'news', 'action' => 'view'));

That are all rules width "/news".

And wenn I call in browser "localhost/news/index.rss", it open News:view(), but not News:index(). If I deactivate the last line, it works, but I need this line.

How can I correct it?

Upvotes: 1

Views: 46

Answers (2)

Andrew
Andrew

Reputation: 13

I accepted the answer from drmonkeyninja quickly and not tested all functions.

I'm pleased width actually functions, but it is not perfect.

What I want, what I have, and what works:

  1. localhost/news/index.rss – It opens RSS-Feed. It works.
  2. localhost/news/, localhost/news/index – It opens all news sorted by publish date width pagignation (AJAX). It works.
  3. localhost/news/{CATEGORY-ID} – It must open news filtered by CATEGORY-ID (integer) sorted by publish date width pagignation (AJAX). It don't work. (It opens localhost/news/view/{NEWS-ID}, see next point.) But it is not important, see "my alternative solution".
  4. localhost/news/{NEWS-TITLE}-{NEWS-ID}, localhost/news/view/{NEWS-ID} – It opens the content of the one news width id={NEWS-ID}. It works. Where {NEWS-TITLE} is string (characters, numbers and '-'), and {NEWS-ID} is integer.

My code:

routes.php

// RSS-Feed. See point 1.
Router::connect('/news/index.rss', array('controller' => 'news', 'action' => 'index', 'ext' => 'rss'));
// See point 2. localhost/news/index
Router::connect('/news/index', array('controller' => 'news', 'action' => 'index'));
// It is for other methode. It works, and it isn't interesting.
Router::connect('/news/indexForPage/*', array('controller' => 'news', 'action' => 'indexForPage'));
// It is easy. localhost/news/view/{NEWS-ID} See point 4.
Router::connect('/news/view/*', array('controller' => 'news', 'action' => 'view'));
// localhost/news/{NEWS-TITLE}-{NEWS-ID} See point 4.
Router::connect('/news/*', array('controller' => 'news', 'action' => 'view'));
// My alternative solution for point 3. (e.g. localhost/breaking-news/3 It works fine with filter and AJAX-pagignation.)
Router::connect('/breaking-news/*/:slug/:page',
        array(
                'controller' => 'news',
                'action' => 'index'
        ),
        array(
                'competence_id'=>'[0-9]+',
                'page'=>'[a-z,0-9,A-Z,\-]+'
        )
);

NewsController.php

class NewsController extends AppController {
public $helpers = array('Paginator');
public $components = array('RequestHandler', 'Paginator');

/**
* See points 1-3.
* @param string $competence_id
*/
public function index($competence_id = NULL) {...}

/**
* It isn't interesting.
* @param int $count
* @param int $sector
*/
public function indexForPage($count = NULL, $sector = NULL) {...}

/**
* See point 4.
* @param string $id
*/
public function view($id = NULL) {...}
}

Points for betterment are welcome:

  • New rooles in routes.php for cases localhost/news/{integer} (point 3) and localhost/news/{string}-{integer} (point 4)
  • Alternative solutions

Upvotes: 0

drmonkeyninja
drmonkeyninja

Reputation: 8540

You need to add 'ext' => 'rss' to your route as you are using Router::parseExtensions('rss');:-

Router::connect(
    '/news/index.rss', 
    array('controller' => 'news', 'action' => 'index', 'ext' => 'rss')
);

Upvotes: 1

Related Questions