默认的 RESTful CakePHP 路由给出缺少控制器方法错误


Default RESTful CakePHP routing gives Missing Controller Method error

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

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

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

在我的每个控制器中,我都包含了 RequestHandler 组件,并在 beforeFilter() 回调中将内容设置为 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/数据视图只是回显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() {
    }
}

有谁知道我做错了什么? 任何帮助不胜感激!

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

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

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