1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: * @link http://cakephp.org CakePHP(tm) Project
12: * @license http://www.opensource.org/licenses/mit-license.php MIT License
13: */
14:
15: App::uses('View', 'View');
16:
17: /**
18: * A view class that is used for JSON responses.
19: *
20: * By setting the '_serialize' key in your controller, you can specify a view variable
21: * that should be serialized to JSON and used as the response for the request.
22: * This allows you to omit views and layouts if you just need to emit a single view
23: * variable as the JSON response.
24: *
25: * In your controller, you could do the following:
26: *
27: * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
28: *
29: * When the view is rendered, the `$posts` view variable will be serialized
30: * into JSON.
31: *
32: * You can also define `'_serialize'` as an array. This will create a top level object containing
33: * all the named view variables:
34: *
35: * {{{
36: * $this->set(compact('posts', 'users', 'stuff'));
37: * $this->set('_serialize', array('posts', 'users'));
38: * }}}
39: *
40: * The above would generate a JSON object that looks like:
41: *
42: * `{"posts": [...], "users": [...]}`
43: *
44: * If you don't use the `_serialize` key, you will need a view. You can use extended
45: * views to provide layout-like functionality.
46: *
47: * You can also enable JSONP support by setting parameter `_jsonp` to true or a string to specify
48: * custom query string paramater name which will contain the callback function name.
49: *
50: * @package Cake.View
51: * @since CakePHP(tm) v 2.1.0
52: */
53: class JsonView extends View {
54:
55: /**
56: * JSON views are always located in the 'json' sub directory for
57: * controllers' views.
58: *
59: * @var string
60: */
61: public $subDir = 'json';
62:
63: /**
64: * Constructor
65: *
66: * @param Controller $controller
67: */
68: public function __construct(Controller $controller = null) {
69: parent::__construct($controller);
70: if (isset($controller->response) && $controller->response instanceof CakeResponse) {
71: $controller->response->type('json');
72: }
73: }
74:
75: /**
76: * Skip loading helpers if this is a _serialize based view.
77: *
78: * @return void
79: */
80: public function loadHelpers() {
81: if (isset($this->viewVars['_serialize'])) {
82: return;
83: }
84: parent::loadHelpers();
85: }
86:
87: /**
88: * Render a JSON view.
89: *
90: * ### Special parameters
91: * `_serialize` To convert a set of view variables into a JSON response.
92: * Its value can be a string for single variable name or array for multiple names.
93: * You can omit the`_serialize` parameter, and use a normal view + layout as well.
94: * `_jsonp` Enables JSONP support and wraps response in callback function provided in query string.
95: * - Setting it to true enables the default query string parameter "callback".
96: * - Setting it to a string value, uses the provided query string parameter for finding the
97: * JSONP callback name.
98: *
99: * @param string $view The view being rendered.
100: * @param string $layout The layout being rendered.
101: * @return string The rendered view.
102: */
103: public function render($view = null, $layout = null) {
104: $return = null;
105: if (isset($this->viewVars['_serialize'])) {
106: $return = $this->_serialize($this->viewVars['_serialize']);
107: } elseif ($view !== false && $this->_getViewFileName($view)) {
108: $return = parent::render($view, false);
109: }
110:
111: if (!empty($this->viewVars['_jsonp'])) {
112: $jsonpParam = $this->viewVars['_jsonp'];
113: if ($this->viewVars['_jsonp'] === true) {
114: $jsonpParam = 'callback';
115: }
116: if (isset($this->request->query[$jsonpParam])) {
117: $return = sprintf('%s(%s)', h($this->request->query[$jsonpParam]), $return);
118: $this->response->type('js');
119: }
120: }
121:
122: return $return;
123: }
124:
125: /**
126: * Serialize view vars
127: *
128: * @param array $serialize The viewVars that need to be serialized
129: * @return string The serialized data
130: */
131: protected function _serialize($serialize) {
132: if (is_array($serialize)) {
133: $data = array();
134: foreach ($serialize as $alias => $key) {
135: if (is_numeric($alias)) {
136: $alias = $key;
137: }
138: if (array_key_exists($key, $this->viewVars)) {
139: $data[$alias] = $this->viewVars[$key];
140: }
141: }
142: $data = !empty($data) ? $data : null;
143: } else {
144: $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
145: }
146:
147: if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) {
148: return json_encode($data, JSON_PRETTY_PRINT);
149: }
150:
151: return json_encode($data);
152: }
153:
154: }
155: