XmlView.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2011, 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-2011, 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. App::uses('Xml', 'Utility');
  15. /**
  16. * A view class that is used for creating XML responses.
  17. *
  18. * By setting the 'serialize' key in your controller, you can specify a view variable
  19. * that should be serialized to XML 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 XML 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 XML.
  29. *
  30. * **Note** The view variable you specify must be compatible with Xml::fromArray().
  31. *
  32. * If you don't use the `serialize` key, you will need a view + layout just like a
  33. * normal view.
  34. *
  35. * @package Cake.View
  36. * @since CakePHP(tm) v 2.1.0
  37. */
  38. class XmlView extends View {
  39. /**
  40. * The subdirectory. XML views are always in xml.
  41. *
  42. * @var string
  43. */
  44. public $subDir = 'xml';
  45. /**
  46. * Constructor
  47. *
  48. * @param Controller $controller
  49. */
  50. public function __construct($controller) {
  51. parent::__construct($controller);
  52. if (is_object($controller)) {
  53. $controller->response->type('xml');
  54. }
  55. }
  56. /**
  57. * Render a XML view.
  58. *
  59. * Uses the special 'serialize' parameter to convert a set of
  60. * view variables into a XML response. Makes generating simple
  61. * XML responses very easy. You can omit the 'serialize' parameter,
  62. * and use a normal view + layout as well.
  63. *
  64. * @param string $view The view being rendered.
  65. * @param string $layout The layout being rendered.
  66. * @return string The rendered view.
  67. */
  68. public function render($view = null, $layout = null) {
  69. if (isset($this->viewVars['serialize'])) {
  70. $serialize = $this->viewVars['serialize'];
  71. $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  72. return $this->output = Xml::fromArray($data)->asXML();
  73. }
  74. if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
  75. return $this->output = $this->_render($viewFileName);
  76. }
  77. }
  78. }