我想为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()。如果我停用最后一行,它可以工作,但我需要这一行。
我该如何纠正?
答案 0 :(得分:1)
您需要在使用'ext' => 'rss'
时向路线添加Router::parseExtensions('rss');
: -
Router::connect(
'/news/index.rss',
array('controller' => 'news', 'action' => 'index', 'ext' => 'rss')
);
答案 1 :(得分:0)
我很快接受了drmonkeyninja的回答而没有测试所有功能。
我很高兴宽度实际上是功能,但它并不完美。
我想要什么,我拥有什么,以及什么有效:
我的代码:
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) {...}
}
欢迎提出改善点: