pictcode / lib / Cake / View / JsonView.php @ master
履歴 | 表示 | アノテート | ダウンロード (5.273 KB)
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 Controller instance.
|
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 |
* ### Special parameters
|
129 |
* `_jsonOptions` You can set custom options for json_encode() this way,
|
130 |
* e.g. `JSON_HEX_TAG | JSON_HEX_APOS`.
|
131 |
*
|
132 |
* @param array $serialize The viewVars that need to be serialized
|
133 |
* @throws CakeException
|
134 |
* @return string The serialized data
|
135 |
*/
|
136 |
protected function _serialize($serialize) { |
137 |
if (is_array($serialize)) { |
138 |
$data = array(); |
139 |
foreach ($serialize as $alias => $key) { |
140 |
if (is_numeric($alias)) { |
141 |
$alias = $key; |
142 |
} |
143 |
if (array_key_exists($key, $this->viewVars)) { |
144 |
$data[$alias] = $this->viewVars[$key]; |
145 |
} |
146 |
} |
147 |
$data = !empty($data) ? $data : null; |
148 |
} else {
|
149 |
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null; |
150 |
} |
151 |
|
152 |
$jsonOptions = 0; |
153 |
if (isset($this->viewVars['_jsonOptions'])) { |
154 |
if ($this->viewVars['_jsonOptions'] === false) { |
155 |
$jsonOptions = 0; |
156 |
} else {
|
157 |
$jsonOptions = $this->viewVars['_jsonOptions']; |
158 |
} |
159 |
} |
160 |
if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) { |
161 |
$jsonOptions = $jsonOptions | JSON_PRETTY_PRINT; |
162 |
} |
163 |
|
164 |
$json = json_encode($data, $jsonOptions); |
165 |
|
166 |
if (function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE) { |
167 |
throw new CakeException(json_last_error_msg()); |
168 |
} |
169 |
if ($json === false) { |
170 |
throw new CakeException('Failed to parse JSON'); |
171 |
} |
172 |
return $json; |
173 |
} |
174 |
|
175 |
} |