CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • Helper
  • HelperCollection
  • JsonView
  • MediaView
  • ScaffoldView
  • ThemeView
  • View
  • ViewBlock
  • XmlView
  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: 
 14: App::uses('View', 'View');
 15: App::uses('Xml', 'Utility');
 16: 
 17: /**
 18:  * A view class that is used for creating XML responses.
 19:  *
 20:  * By setting the '_serialize' key in your controller, you can specify a view variable
 21:  * that should be serialized to XML and used as the response for the request.
 22:  * This allows you to omit views + layouts, if your just need to emit a single view
 23:  * variable as the XML response.
 24:  *
 25:  * In your controller, you could do the following:
 26:  *
 27:  * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
 28:  *
 29:  * When the view is rendered, the `$posts` view variable will be serialized
 30:  * into XML.
 31:  *
 32:  * **Note** The view variable you specify must be compatible with Xml::fromArray().
 33:  *
 34:  * You can also define `'_serialize'` as an array.  This will create an additional
 35:  * top level element named `<response>` containing all the named view variables:
 36:  *
 37:  * {{{
 38:  * $this->set(compact('posts', 'users', 'stuff'));
 39:  * $this->set('_serialize', array('posts', 'users'));
 40:  * }}}
 41:  *
 42:  * The above would generate a XML object that looks like:
 43:  *
 44:  * `<response><posts>...</posts><users>...</users></response>`
 45:  *
 46:  * If you don't use the `_serialize` key, you will need a view.  You can use extended
 47:  * views to provide layout like functionality.
 48:  *
 49:  * @package       Cake.View
 50:  * @since         CakePHP(tm) v 2.1.0
 51:  */
 52: class XmlView extends View {
 53: 
 54: /**
 55:  * The subdirectory.  XML views are always in xml.
 56:  *
 57:  * @var string
 58:  */
 59:     public $subDir = 'xml';
 60: 
 61: /**
 62:  * Constructor
 63:  *
 64:  * @param Controller $controller
 65:  */
 66:     public function __construct(Controller $controller = null) {
 67:         parent::__construct($controller);
 68: 
 69:         if (isset($controller->response) && $controller->response instanceof CakeResponse) {
 70:             $controller->response->type('xml');
 71:         }
 72:     }
 73: 
 74: /**
 75:  * Render a XML view.
 76:  *
 77:  * Uses the special '_serialize' parameter to convert a set of
 78:  * view variables into a XML response.  Makes generating simple
 79:  * XML responses very easy.  You can omit the '_serialize' parameter,
 80:  * and use a normal view + layout as well.
 81:  *
 82:  * @param string $view The view being rendered.
 83:  * @param string $layout The layout being rendered.
 84:  * @return string The rendered view.
 85:  */
 86:     public function render($view = null, $layout = null) {
 87:         if (isset($this->viewVars['_serialize'])) {
 88:             $serialize = $this->viewVars['_serialize'];
 89:             if (is_array($serialize)) {
 90:                 $data = array('response' => array());
 91:                 foreach ($serialize as $key) {
 92:                     $data['response'][$key] = $this->viewVars[$key];
 93:                 }
 94:             } else {
 95:                 $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
 96:                 if (is_array($data) && Set::numeric(array_keys($data))) {
 97:                     $data = array('response' => array($serialize => $data));
 98:                 }
 99:             }
100:             $content = Xml::fromArray($data)->asXML();
101:             return $content;
102:         }
103:         if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
104:             if (!$this->_helpersLoaded) {
105:                 $this->loadHelpers();
106:             }
107:             $content = $this->_render($viewFileName);
108:             $this->Blocks->set('content', (string)$content);
109:             return $content;
110:         }
111:     }
112: 
113: }
114: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs