默认的RESTful CakePHP路由会导致Missing Controller Method错误

时间:2012-04-30 03:01:11

标签: php cakephp rest

我正在按照本教程http://book.cakephp.org/2.0/en/development/rest.html在CakePHP应用程序中启动并运行REST。

我已将以下内容添加到我的routes.php文件中:

Router::mapResources(array('fantasyPicks', 'fantasyPlayers'));
Router::parseExtensions();

在我的每个控制器中,我在beforeFilter()回调中包含了RequestHandler组件和setContent到json。它看起来像这样:

class FantasyPicksController extends AppController {
public $components = array('RequestHandler');

public function beforeFilter() {
    parent::beforeFilter();
    $this->RequestHandler->setContent('json','text/x-json');
    $this->layout = 'json/default';
}

public function index() {
    $fantasyPicks = $this->FantasyPick->find('all');
    $this->set('json', $fantasyPicks);
    $this->render('/json/data');
}
...

我的json / data视图只是echos json_encode:

<?php echo json_encode($json); ?>

完成所有这些后,导航到/ fantasyPicks / view / 1按预期工作。但是,/ fantasyPicks / 1给出了以下错误:

Missing Method in FantasyPicksController
Error: The action 1 is not defined in controller FantasyPicksController

Error: Create FantasyPicksController::1() in file: app\Controller\FantasyPicksController.php.

<?php
class FantasyPicksController extends AppController {

    public function 1() {

    }

}

有谁知道我做错了什么?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

访问页面时必须使用正确的控制器命名约定。

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

请参阅URL注意事项部分。所以你应该去/ fantasy_picks / 1,它会正常工作。