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

如何将cakephp2中的路由配置为index.rss,cakephp,Cakephp,我想为RSS视图编写新的路由。调用/news/index.rss必须打开rss提要,但它打开的方法错误 routes.php Router::parseExtensions('rss'); ... // don't work Router::connect('/news/index.rss', array('controller' => 'news', 'action' => 'index')); ... // open News:indexForPage() Router::con

我想为RSS视图编写新的路由。调用/news/index.rss必须打开rss提要,但它打开的方法错误

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'));
// 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,\-]+'
        )
);
这些都是新闻规则

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


如何更正它?

当您使用
路由器::解析扩展(“rss”)时,需要将
'ext'=>'rss'
添加到路由中:-

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

我很快接受了Monkeynija博士的答案,并没有测试所有的功能

我很高兴width的实际功能,但它并不完美

我想要什么,我拥有什么,什么有用:

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

    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'));
    
    // 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中用于localhost/news/{integer}(第3点)和localhost/news/{string}-{integer}(第4点)的新rooles
    • 替代解决方案

    不客气。您能否接受我的回答,以便其他有类似问题的人能够快速看到解决方案。:-)