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 + layouts, if your 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: * @package Cake.View
48: * @since CakePHP(tm) v 2.1.0
49: */
50: class JsonView extends View {
51:
52: /**
53: * JSON views are always located in the 'json' sub directory for a
54: * controllers views.
55: *
56: * @var string
57: */
58: public $subDir = 'json';
59:
60: /**
61: * Constructor
62: *
63: * @param Controller $controller
64: */
65: public function __construct(Controller $controller = null) {
66: parent::__construct($controller);
67: if (isset($controller->response) && $controller->response instanceof CakeResponse) {
68: $controller->response->type('json');
69: }
70: }
71:
72: /**
73: * Render a JSON view.
74: *
75: * Uses the special '_serialize' parameter to convert a set of
76: * view variables into a JSON response. Makes generating simple
77: * JSON responses very easy. You can omit the '_serialize' parameter,
78: * and use a normal view + layout as well.
79: *
80: * @param string $view The view being rendered.
81: * @param string $layout The layout being rendered.
82: * @return string The rendered view.
83: */
84: public function render($view = null, $layout = null) {
85: if (isset($this->viewVars['_serialize'])) {
86: return $this->_serialize($this->viewVars['_serialize']);
87: }
88: if ($view !== false && $this->_getViewFileName($view)) {
89: return parent::render($view, false);
90: }
91: }
92:
93: /**
94: * Serialize view vars
95: *
96: * @param array $serialize The viewVars that need to be serialized
97: * @return string The serialized data
98: */
99: protected function _serialize($serialize) {
100: if (is_array($serialize)) {
101: $data = array();
102: foreach ($serialize as $key) {
103: $data[$key] = $this->viewVars[$key];
104: }
105: } else {
106: $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
107: }
108: return json_encode($data);
109: }
110:
111: }
112: