0

I am using CakePHP 3.1 and trying to render the json index response for User model. I followed the CakePHP manual page and created index.ctp under the directory src/Template/Users/json/, but the file does not get hooked by the controller. I also tried src/View/Users, which also did not work. Should I specify a file to render or something else? In src/Controller/UsersController.php, I have index() method with

$users = $this->paginate('Users'); $this->set(compact('users'));

taro
  • 699
  • 1
  • 9
  • 20
  • Did you also [add the json extension in the Routing](http://book.cakephp.org/3.0/en/development/routing.html#routing-file-extensions)? – Oops D'oh Oct 08 '15 at 21:36

1 Answers1

2

Check your AppController.php. Since version 3.1.0 this sets the _serialize to true by default. Therefore the template files are all ignored.

public function beforeRender(Event $event)
{
    if (!array_key_exists('_serialize', $this->viewVars) &&
        in_array($this->response->type(), ['application/json', 'application/xml'])
    ) {
        $this->set('_serialize', true);
    }
}

You need to remove that code from beforeRender.

MuhKuh
  • 1,049
  • 10
  • 18