1

我正在按照本教程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 视图只是回显 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() {

    }

}

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

4

1 回答 1

1

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

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

Refer to the URL considerations section. So you should be going to /fantasy_picks/1 and it will work properly.

于 2012-04-30T03:51:45.347 回答