2011-09-17 01:52:12 +00:00
|
|
|
<?php
|
2011-10-29 02:01:33 +00:00
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 02:01:33 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('View', 'View');
|
2011-09-17 01:52:12 +00:00
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
/**
|
2011-11-07 03:13:35 +00:00
|
|
|
* A view class that is used for JSON responses.
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* By setting the '_serialize' key in your controller, you can specify a view variable
|
2011-11-07 03:13:35 +00:00
|
|
|
* that should be serialized to JSON and used as the response for the request.
|
|
|
|
* This allows you to omit views + layouts, if your just need to emit a single view
|
|
|
|
* variable as the JSON response.
|
|
|
|
*
|
|
|
|
* In your controller, you could do the following:
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
|
2011-11-07 03:13:35 +00:00
|
|
|
*
|
|
|
|
* When the view is rendered, the `$posts` view variable will be serialized
|
|
|
|
* into JSON.
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* You can also define `'_serialize'` as an array. This will create a top level object containing
|
2011-11-18 03:24:49 +00:00
|
|
|
* all the named view variables:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $this->set(compact('posts', 'users', 'stuff'));
|
2011-11-23 01:56:25 +00:00
|
|
|
* $this->set('_serialize', array('posts', 'users'));
|
2011-11-18 03:24:49 +00:00
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* The above would generate a JSON object that looks like:
|
|
|
|
*
|
|
|
|
* `{"posts": [...], "users": [...]}`
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* If you don't use the `_serialize` key, you will need a view. You can use extended
|
2011-11-18 03:24:49 +00:00
|
|
|
* views to provide layout like functionality.
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
|
|
|
* @package Cake.View
|
2011-11-07 03:13:35 +00:00
|
|
|
* @since CakePHP(tm) v 2.1.0
|
2011-10-29 02:01:33 +00:00
|
|
|
*/
|
2011-09-17 01:52:12 +00:00
|
|
|
class JsonView extends View {
|
|
|
|
|
2011-11-08 02:46:52 +00:00
|
|
|
/**
|
|
|
|
* JSON views are always located in the 'json' sub directory for a
|
|
|
|
* controllers views.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $subDir = 'json';
|
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param Controller $controller
|
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function __construct(Controller $controller = null) {
|
2011-11-23 02:02:22 +00:00
|
|
|
parent::__construct($controller);
|
2011-11-29 02:36:13 +00:00
|
|
|
if (isset($controller->response) && $controller->response instanceof CakeResponse) {
|
|
|
|
$controller->response->type('json');
|
2011-10-29 02:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-17 01:52:12 +00:00
|
|
|
/**
|
|
|
|
* Render a JSON view.
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* Uses the special '_serialize' parameter to convert a set of
|
2011-09-17 01:52:12 +00:00
|
|
|
* view variables into a JSON response. Makes generating simple
|
2011-11-23 01:56:25 +00:00
|
|
|
* JSON responses very easy. You can omit the '_serialize' parameter,
|
2011-09-17 01:52:12 +00:00
|
|
|
* and use a normal view + layout as well.
|
|
|
|
*
|
|
|
|
* @param string $view The view being rendered.
|
|
|
|
* @param string $layout The layout being rendered.
|
2011-10-29 02:01:33 +00:00
|
|
|
* @return string The rendered view.
|
2011-09-17 01:52:12 +00:00
|
|
|
*/
|
|
|
|
public function render($view = null, $layout = null) {
|
2011-11-23 01:56:25 +00:00
|
|
|
if (isset($this->viewVars['_serialize'])) {
|
|
|
|
$serialize = $this->viewVars['_serialize'];
|
2011-11-18 03:24:49 +00:00
|
|
|
if (is_array($serialize)) {
|
|
|
|
$data = array();
|
|
|
|
foreach ($serialize as $key) {
|
|
|
|
$data[$key] = $this->viewVars[$key];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
|
|
|
}
|
2011-12-18 04:50:41 +00:00
|
|
|
$content = json_encode($data);
|
|
|
|
$this->Blocks->set('content', $content);
|
|
|
|
return $content;
|
2011-11-07 03:13:35 +00:00
|
|
|
}
|
2011-10-29 02:01:33 +00:00
|
|
|
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
|
2012-01-12 01:21:56 +00:00
|
|
|
if (!$this->_helpersLoaded) {
|
|
|
|
$this->loadHelpers();
|
|
|
|
}
|
2011-12-18 04:50:41 +00:00
|
|
|
$content = $this->_render($viewFileName);
|
|
|
|
$this->Blocks->set('content', $content);
|
|
|
|
return $content;
|
2011-09-17 01:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-29 02:01:33 +00:00
|
|
|
|
2011-09-17 01:52:12 +00:00
|
|
|
}
|