AjaxView.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 MIT
  15. */
  16. class AjaxView extends View {
  17. /**
  18. * The subdirectory. AJAX views are always in ajax.
  19. *
  20. * @var string
  21. */
  22. public $subDir = 'ajax';
  23. /**
  24. * Name of layout to use with this View.
  25. *
  26. * @var string
  27. */
  28. public $layout = false;
  29. /**
  30. * Constructor
  31. *
  32. * @param Controller $controller
  33. */
  34. public function __construct(Controller $controller = null) {
  35. parent::__construct($controller);
  36. // Unfortunately, layout gets overwritten via passed Controller attribute
  37. if ($this->layout === 'default' || $this->layout === 'ajax') {
  38. $this->layout = false;
  39. }
  40. if (isset($controller->response) && $controller->response instanceof CakeResponse) {
  41. $controller->response->type('json');
  42. }
  43. }
  44. /**
  45. * Renders an AJAX view.
  46. * The rendered content will be part of the JSON response object and
  47. * can be accessed via response.content in JavaScript.
  48. *
  49. * If an error has been set, the rendering will be skipped.
  50. *
  51. * @param string $view The view being rendered.
  52. * @param string $layout The layout being rendered.
  53. * @return string The rendered view.
  54. */
  55. public function render($view = null, $layout = null) {
  56. $response = array(
  57. 'error' => null,
  58. 'content' => null,
  59. );
  60. if (!empty($this->viewVars['error'])) {
  61. $view = false;
  62. }
  63. if ($view !== false && $this->_getViewFileName($view)) {
  64. $response['content'] = parent::render($view, $layout);
  65. }
  66. if (isset($this->viewVars['_serialize'])) {
  67. $response = $this->_serialize($response, $this->viewVars['_serialize']);
  68. }
  69. return json_encode($response);
  70. }
  71. /**
  72. * Serializes view vars.
  73. *
  74. * @param array $response Response data array.
  75. * @param array $serialize The viewVars that need to be serialized.
  76. * @return array The serialized data.
  77. */
  78. protected function _serialize($response, $serialize) {
  79. if (is_array($serialize)) {
  80. foreach ($serialize as $alias => $key) {
  81. if (is_numeric($alias)) {
  82. $alias = $key;
  83. }
  84. if (array_key_exists($key, $this->viewVars)) {
  85. $response[$alias] = $this->viewVars[$key];
  86. }
  87. }
  88. } else {
  89. $response[$serialize] = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  90. }
  91. return $response;
  92. }
  93. }