JsonView.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. App::uses('View', 'View');
  15. /**
  16. * A view class that is used for JSON responses.
  17. *
  18. * By setting the '_serialize' key in your controller, you can specify a view variable
  19. * that should be serialized to JSON and used as the response for the request.
  20. * This allows you to omit views + layouts, if your just need to emit a single view
  21. * variable as the JSON response.
  22. *
  23. * In your controller, you could do the following:
  24. *
  25. * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
  26. *
  27. * When the view is rendered, the `$posts` view variable will be serialized
  28. * into JSON.
  29. *
  30. * You can also define `'_serialize'` as an array. This will create a top level object containing
  31. * all the named view variables:
  32. *
  33. * {{{
  34. * $this->set(compact('posts', 'users', 'stuff'));
  35. * $this->set('_serialize', array('posts', 'users'));
  36. * }}}
  37. *
  38. * The above would generate a JSON object that looks like:
  39. *
  40. * `{"posts": [...], "users": [...]}`
  41. *
  42. * If you don't use the `_serialize` key, you will need a view. You can use extended
  43. * views to provide layout like functionality.
  44. *
  45. * You can also enable JSONP support by setting parameter `_jsonp` to true or a string to specify
  46. * custom query string paramater name which will contain the callback function name.
  47. *
  48. * @package Cake.View
  49. * @since CakePHP(tm) v 2.1.0
  50. */
  51. class JsonView extends View {
  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. * 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. * Render a JSON view.
  72. *
  73. * ### Special parameters
  74. * `_serialize` To convert a set of view variables into a JSON response.
  75. * It's value can be a string for single variable name or array for multiple names.
  76. * You can omit the`_serialize` parameter, and use a normal view + layout as well.
  77. * `_jsonp` Enables JSONP support and wraps response in callback function provided in query string.
  78. * - Setting it to true enables the default query string parameter "callback".
  79. * - Setting it to a string value, uses the provided query string parameter for finding the
  80. * JSONP callback name.
  81. *
  82. * @param string $view The view being rendered.
  83. * @param string $layout The layout being rendered.
  84. * @return string The rendered view.
  85. */
  86. public function render($view = null, $layout = null) {
  87. $return = null;
  88. if (isset($this->viewVars['_serialize'])) {
  89. $return = $this->_serialize($this->viewVars['_serialize']);
  90. } elseif ($view !== false && $this->_getViewFileName($view)) {
  91. $return = parent::render($view, false);
  92. }
  93. if (!empty($this->viewVars['_jsonp'])) {
  94. $jsonpParam = $this->viewVars['_jsonp'];
  95. if ($this->viewVars['_jsonp'] === true) {
  96. $jsonpParam = 'callback';
  97. }
  98. if (isset($this->request->query[$jsonpParam])) {
  99. $return = sprintf('%s(%s)', h($this->request->query[$jsonpParam]), $return);
  100. $this->response->type('js');
  101. }
  102. }
  103. return $return;
  104. }
  105. /**
  106. * Serialize view vars
  107. *
  108. * @param array $serialize The viewVars that need to be serialized
  109. * @return string The serialized data
  110. */
  111. protected function _serialize($serialize) {
  112. if (is_array($serialize)) {
  113. $data = array();
  114. foreach ($serialize as $key) {
  115. $data[$key] = $this->viewVars[$key];
  116. }
  117. } else {
  118. $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  119. }
  120. return json_encode($data);
  121. }
  122. }