JsonView (class)
Class JsonView
A view class that is used for JSON responses.
By setting the '_serialize' key in your controller, you can specify a view variable that should be serialized to JSON and used as the response for the request. This allows you to omit views and layouts if you just need to emit a single view variable as the JSON response.
In your controller, you could do the following:
$this->set(array('posts' => $posts, '_serialize' => 'posts'));
When the view is rendered, the $posts
view variable will be serialized into JSON.
You can also define '_serialize'
as an array. This will create a top level object containing all the named view variables:
$this->set(compact('posts', 'users', 'stuff')); $