JsonView.php 3.2 KB

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