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)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-10-29 02:01:33 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 02:01:33 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-10-29 02:01:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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.
|
2013-09-18 00:15:18 +00:00
|
|
|
* This allows you to omit views and layouts if you just need to emit a single view
|
2011-11-07 03:13:35 +00:00
|
|
|
* 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
|
|
|
*
|
2012-07-18 01:55:29 +00:00
|
|
|
* When the view is rendered, the `$posts` view variable will be serialized
|
2011-11-07 03:13:35 +00:00
|
|
|
* into JSON.
|
|
|
|
*
|
2012-12-22 22:48:15 +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:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-11-18 03:24:49 +00:00
|
|
|
* $this->set(compact('posts', 'users', 'stuff'));
|
2011-11-23 01:56:25 +00:00
|
|
|
* $this->set('_serialize', array('posts', 'users'));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2011-11-18 03:24:49 +00:00
|
|
|
* The above would generate a JSON object that looks like:
|
|
|
|
*
|
|
|
|
* `{"posts": [...], "users": [...]}`
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* If you don't use the `_serialize` key, you will need a view. You can use extended
|
2013-09-18 00:15:18 +00:00
|
|
|
* views to provide layout-like functionality.
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
2013-02-02 07:55:27 +00:00
|
|
|
* You can also enable JSONP support by setting parameter `_jsonp` to true or a string to specify
|
|
|
|
* custom query string paramater name which will contain the callback function name.
|
|
|
|
*
|
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
|
|
|
/**
|
2013-09-18 00:25:32 +00:00
|
|
|
* JSON views are always located in the 'json' sub directory for
|
|
|
|
* controllers' views.
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2011-11-08 02:46:52 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $subDir = 'json';
|
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param Controller $controller Controller instance.
|
2011-10-29 02:01:33 +00:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 14:00:29 +00:00
|
|
|
/**
|
|
|
|
* Skip loading helpers if this is a _serialize based view.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function loadHelpers() {
|
|
|
|
if (isset($this->viewVars['_serialize'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parent::loadHelpers();
|
|
|
|
}
|
|
|
|
|
2011-09-17 01:52:12 +00:00
|
|
|
/**
|
|
|
|
* Render a JSON view.
|
|
|
|
*
|
2013-02-02 07:55:27 +00:00
|
|
|
* ### Special parameters
|
|
|
|
* `_serialize` To convert a set of view variables into a JSON response.
|
2013-09-18 00:15:18 +00:00
|
|
|
* Its value can be a string for single variable name or array for multiple names.
|
2013-02-02 07:55:27 +00:00
|
|
|
* You can omit the`_serialize` parameter, and use a normal view + layout as well.
|
|
|
|
* `_jsonp` Enables JSONP support and wraps response in callback function provided in query string.
|
|
|
|
* - Setting it to true enables the default query string parameter "callback".
|
|
|
|
* - Setting it to a string value, uses the provided query string parameter for finding the
|
|
|
|
* JSONP callback name.
|
2011-09-17 01:52:12 +00:00
|
|
|
*
|
|
|
|
* @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) {
|
2013-02-02 07:55:27 +00:00
|
|
|
$return = null;
|
2011-11-23 01:56:25 +00:00
|
|
|
if (isset($this->viewVars['_serialize'])) {
|
2013-02-02 07:55:27 +00:00
|
|
|
$return = $this->_serialize($this->viewVars['_serialize']);
|
|
|
|
} elseif ($view !== false && $this->_getViewFileName($view)) {
|
|
|
|
$return = parent::render($view, false);
|
2011-11-07 03:13:35 +00:00
|
|
|
}
|
2013-02-02 07:55:27 +00:00
|
|
|
|
|
|
|
if (!empty($this->viewVars['_jsonp'])) {
|
|
|
|
$jsonpParam = $this->viewVars['_jsonp'];
|
|
|
|
if ($this->viewVars['_jsonp'] === true) {
|
|
|
|
$jsonpParam = 'callback';
|
|
|
|
}
|
|
|
|
if (isset($this->request->query[$jsonpParam])) {
|
|
|
|
$return = sprintf('%s(%s)', h($this->request->query[$jsonpParam]), $return);
|
|
|
|
$this->response->type('js');
|
|
|
|
}
|
2011-09-17 01:52:12 +00:00
|
|
|
}
|
2013-02-02 07:55:27 +00:00
|
|
|
|
|
|
|
return $return;
|
2011-09-17 01:52:12 +00:00
|
|
|
}
|
2011-10-29 02:01:33 +00:00
|
|
|
|
2012-08-10 07:54:05 +00:00
|
|
|
/**
|
|
|
|
* Serialize view vars
|
2015-03-13 16:12:21 +00:00
|
|
|
* For JSON documents, any errors are output using;
|
|
|
|
* PHP 5 >= 5.5.0 : json_last_error_msg()
|
|
|
|
* PHP 5 >= 5.3.0 : json_last_error()
|
|
|
|
* PHP 5 <= 5.2.9 : Generic fall back error
|
2012-08-10 07:54:05 +00:00
|
|
|
*
|
|
|
|
* @param array $serialize The viewVars that need to be serialized
|
2015-03-14 08:45:18 +00:00
|
|
|
* @throws CakeException
|
2012-08-10 07:54:05 +00:00
|
|
|
* @return string The serialized data
|
|
|
|
*/
|
|
|
|
protected function _serialize($serialize) {
|
|
|
|
if (is_array($serialize)) {
|
|
|
|
$data = array();
|
2013-07-06 16:57:46 +00:00
|
|
|
foreach ($serialize as $alias => $key) {
|
|
|
|
if (is_numeric($alias)) {
|
|
|
|
$alias = $key;
|
|
|
|
}
|
2013-09-05 16:46:55 +00:00
|
|
|
if (array_key_exists($key, $this->viewVars)) {
|
|
|
|
$data[$alias] = $this->viewVars[$key];
|
|
|
|
}
|
2012-08-10 07:54:05 +00:00
|
|
|
}
|
2013-09-05 16:46:55 +00:00
|
|
|
$data = !empty($data) ? $data : null;
|
2012-08-10 07:54:05 +00:00
|
|
|
} else {
|
|
|
|
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
|
|
|
}
|
2013-08-02 13:39:54 +00:00
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) {
|
2015-03-14 09:13:31 +00:00
|
|
|
$json = json_encode($data, JSON_PRETTY_PRINT);
|
2015-03-13 16:12:21 +00:00
|
|
|
} else {
|
2015-03-14 09:13:31 +00:00
|
|
|
$json = json_encode($data);
|
2013-08-02 13:39:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 16:12:21 +00:00
|
|
|
if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
if (function_exists('json_last_error_msg')) {
|
|
|
|
$error = json_last_error_msg();
|
|
|
|
} else {
|
2015-03-14 08:34:06 +00:00
|
|
|
$error = sprintf('JSON encoding failed: Error code %s', json_last_error());
|
2015-03-13 16:12:21 +00:00
|
|
|
}
|
|
|
|
throw new CakeException($error);
|
2015-03-13 16:27:24 +00:00
|
|
|
} elseif ($json === false) {
|
2015-03-14 08:34:06 +00:00
|
|
|
throw new CakeException('Failed to parse JSON');
|
2015-03-13 16:12:21 +00:00
|
|
|
}
|
|
|
|
return $json;
|
2012-08-10 07:54:05 +00:00
|
|
|
}
|
|
|
|
|
2011-09-17 01:52:12 +00:00
|
|
|
}
|