蛋糕PHP restful 索引方法returns 空响应

Cake PHP restful index method returns empty response

我开始学习 CakePhp。我正在尝试创建一个 restful 应用程序,每当我通过 url http://localhost:8888/todolist/entries/index.json 打开浏览器时,我都没有收到 json 响应。

   <?php
    App::uses('AppController', 'Controller');
    /**
     * Entries Controller
     *
     * @property Entry $Entry
     * @property PaginatorComponent $Paginator
     */
    class EntriesController extends AppController {

        /**
         * @var array
         */
        public $helpers = array('Html', 'Form');

    /**
     * Components
     *
     * @var array
     */
        public $components = array('Paginator', 'RequestHandler');

    /**
     * index method
     *
     * @return void
     */
        public function index() {
            $this->Entry->recursive = 0;
            $this->set('entries', $this->Paginator->paginate());

            $this->autoRender = false;
            $this->layout = false;
        }
}

这是我的 routes.php:

    Router::connect('/', array('controller' => 'entries', 'action' => 'index'));
/**
 * ...and connect the rest of 'Pages' controller's URLs.
 */
    Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

/**
 * Load all plugin routes. See the CakePlugin documentation on
 * how to customize the loading of plugin routes.
 */
    CakePlugin::routes();

/**
 * Load the CakePHP default routes. Only remove this if you do not want to use
 * the built-in default routes.
 */
    require CAKE . 'Config' . DS . 'routes.php';

Router::mapResources('entries');
Router::parseExtensions('json');

下面是来自 firebug 的屏幕截图。

您已禁用渲染:

$this->autoRender = false;

因此您将得到一个空的响应。无需禁用渲染,使用 REST 和 JSON/XML 视图文档中描述的序列化视图:

public function index() {
    $this->Entry->recursive = 0;
    $this->set('entries', $this->Paginator->paginate());
    $this->set('_serialize', 'entries'); // that's where the magic begins
}

另见