1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10: * @link http://cakephp.org CakePHP(tm) Project
11: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12: */
13:
14: App::uses('View', 'View');
15:
16: /**
17: * A view class that is used for JSON responses.
18: *
19: * By setting the '_serialize' key in your controller, you can specify a view variable
20: * that should be serialized to JSON and used as the response for the request.
21: * This allows you to omit views + layouts, if your just need to emit a single view
22: * variable as the JSON response.
23: *
24: * In your controller, you could do the following:
25: *
26: * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
27: *
28: * When the view is rendered, the `$posts` view variable will be serialized
29: * into JSON.
30: *
31: * You can also define `'_serialize'` as an array. This will create a top level object containing
32: * all the named view variables:
33: *
34: * {{{
35: * $this->set(compact('posts', 'users', 'stuff'));
36: * $this->set('_serialize', array('posts', 'users'));
37: * }}}
38: *
39: * The above would generate a JSON object that looks like:
40: *
41: * `{"posts": [...], "users": [...]}`
42: *
43: * If you don't use the `_serialize` key, you will need a view. You can use extended
44: * views to provide layout like functionality.
45: *
46: * @package Cake.View
47: * @since CakePHP(tm) v 2.1.0
48: */
49: class JsonView extends View {
50:
51: /**
52: * JSON views are always located in the 'json' sub directory for a
53: * controllers views.
54: *
55: * @var string
56: */
57: public $subDir = 'json';
58:
59: /**
60: * Constructor
61: *
62: * @param Controller $controller
63: */
64: public function __construct(Controller $controller = null) {
65: parent::__construct($controller);
66: if (isset($controller->response) && $controller->response instanceof CakeResponse) {
67: $controller->response->type('json');
68: }
69: }
70:
71: /**
72: * Render a JSON view.
73: *
74: * Uses the special '_serialize' parameter to convert a set of
75: * view variables into a JSON response. Makes generating simple
76: * JSON responses very easy. You can omit the '_serialize' parameter,
77: * and use a normal view + layout as well.
78: *
79: * @param string $view The view being rendered.
80: * @param string $layout The layout being rendered.
81: * @return string The rendered view.
82: */
83: public function render($view = null, $layout = null) {
84: if (isset($this->viewVars['_serialize'])) {
85: $serialize = $this->viewVars['_serialize'];
86: if (is_array($serialize)) {
87: $data = array();
88: foreach ($serialize as $key) {
89: $data[$key] = $this->viewVars[$key];
90: }
91: } else {
92: $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
93: }
94: $content = json_encode($data);
95: $this->Blocks->set('content', $content);
96: return $content;
97: }
98: if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
99: if (!$this->_helpersLoaded) {
100: $this->loadHelpers();
101: }
102: $content = $this->_render($viewFileName);
103: $this->Blocks->set('content', $content);
104: return $content;
105: }
106: }
107:
108: }
109: