JsonView.php 3.2 KB

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