AjaxView.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. App::uses('View', 'View');
  3. /**
  4. * A view to handle AJAX requests.
  5. *
  6. * Expects all incoming requests to be of extension "json" and that the expected result
  7. * will also be in JSON format.
  8. * A response to an invalid request may be just HTTP status "code" and error "message"
  9. * (e.g, on 4xx or 5xx).
  10. * A response to a valid request will always contain at least "content" and "error" keys.
  11. * You can add more data using _serialize.
  12. *
  13. * @author Mark Scherer
  14. * @license http://opensource.org/licenses/mit-license.php MIT
  15. */
  16. class AjaxView extends View {
  17. /**
  18. * List of variables to collect from the associated controller.
  19. *
  20. * @var array
  21. */
  22. protected $_passedVars = [
  23. 'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme',
  24. 'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction', 'subDir'
  25. ];
  26. /**
  27. * The subdirectory. AJAX views are always in ajax.
  28. *
  29. * @var string
  30. */
  31. public $subDir = 'ajax';
  32. /**
  33. * Name of layout to use with this View.
  34. *
  35. * @var string
  36. */
  37. public $layout = false;
  38. /**
  39. * Constructor
  40. *
  41. * @param Controller $controller
  42. */
  43. public function __construct(Controller $controller = null) {
  44. parent::__construct($controller);
  45. // Unfortunately, layout gets overwritten via passed Controller attribute
  46. if ($this->layout === 'default' || $this->layout === 'ajax') {
  47. $this->layout = false;
  48. }
  49. if ($this->subDir === null) {
  50. $this->subDir = 'ajax';
  51. }
  52. if (isset($controller->response) && $controller->response instanceof CakeResponse) {
  53. $controller->response->type('json');
  54. }
  55. }
  56. /**
  57. * Renders an AJAX view.
  58. * The rendered content will be part of the JSON response object and
  59. * can be accessed via response.content in JavaScript.
  60. *
  61. * If an error has been set, the rendering will be skipped.
  62. *
  63. * @param string $view The view being rendered.
  64. * @param string $layout The layout being rendered.
  65. * @return string The rendered view.
  66. */
  67. public function render($view = null, $layout = null) {
  68. $response = [
  69. 'error' => null,
  70. 'content' => null,
  71. ];
  72. if (!empty($this->viewVars['error'])) {
  73. $view = false;
  74. }
  75. if ($view !== false && !isset($this->viewVars['_redirect']) && $this->_getViewFileName($view)) {
  76. $response['content'] = parent::render($view, $layout);
  77. }
  78. if (isset($this->viewVars['_serialize'])) {
  79. $response = $this->_serialize($response, $this->viewVars['_serialize']);
  80. }
  81. return json_encode($response);
  82. }
  83. /**
  84. * Serializes view vars.
  85. *
  86. * @param array $response Response data array.
  87. * @param array $serialize The viewVars that need to be serialized.
  88. * @return array The serialized data.
  89. */
  90. protected function _serialize($response, $serialize) {
  91. if (is_array($serialize)) {
  92. foreach ($serialize as $alias => $key) {
  93. if (is_numeric($alias)) {
  94. $alias = $key;
  95. }
  96. if (array_key_exists($key, $this->viewVars)) {
  97. $response[$alias] = $this->viewVars[$key];
  98. }
  99. }
  100. } else {
  101. $response[$serialize] = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  102. }
  103. return $response;
  104. }
  105. }