如何将cakephp2中的路由配置为index.rss

时间:2015-12-01 16:31:18

标签: cakephp

我想为RSS-View编写新的路由。调用/news/index.rss必须打开RSS-Feed,但它打开了错误的方法。

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'));

这都是规则宽度“/ news”。

而且我在浏览器中调用“localhost / news / index.rss”,它打开News:view(),但不打开News:index()。如果我停用最后一行,它可以工作,但我需要这一行。

我该如何纠正?

2 个答案:

答案 0 :(得分:1)

您需要在使用'ext' => 'rss'时向路线添加Router::parseExtensions('rss');: -

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

答案 1 :(得分:0)

我很快接受了drmonkeyninja的回答而没有测试所有功能。

我很高兴宽度实际上是功能,但它并不完美。

我想要什么,我拥有什么,以及什么有效:

  1. localhost / news / index.rss - 它打开RSS-Feed。它有效。
  2. localhost / news /,localhost / news / index - 它打开按发布日期宽度pagignation(AJAX)排序的所有新闻。它有效。
  3. localhost / news / {CATEGORY-ID} - 必须打开按发布日期宽度分页(AJAX)排序的CATEGORY-ID(整数)过滤的新闻。它不起作用。 (它打开localhost / news / view / {NEWS-ID},请参阅下一点。)但这并不重要,请参阅“我的替代解决方案”。
  4. localhost / news / {NEWS-TITLE} - {NEWS-ID},localhost / news / view / {NEWS-ID} - 它打开一个新闻宽度id = {NEWS-ID}的内容。有用。其中{NEWS-TITLE}是字符串(字符,数字和' - '),{NEWS-ID}是整数。
  5. 我的代码:

    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) {...}
    }
    

    欢迎提出改善点:

    • routes.php中的新rooles for localhost / news / {integer}(第3点)和localhost / news / {string} - {integer}(第4点)
    • 替代解决方案