greentech
greentech

Reputation: 111

Cake PHP restful index method returns empty response

I started to learn CakePhp. I am trying to make a restful application an whenever I open a browser via url http://localhost:8888/todolist/entries/index.json I get no json response.

   <?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;
        }
}

This is my 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');

Below is a screenshot from firebug.

Firebug Network screenshot

Upvotes: 0

Views: 598

Answers (1)

ndm
ndm

Reputation: 60463

You've disabled rendering:

$this->autoRender = false;

hence you'll get an empty response. No need to disable rendering, use a serialized view as described in the REST and JSON/XML view docs:

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

See also

Upvotes: 1

Related Questions