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.4 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 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
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • 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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  * @link          http://cakephp.org CakePHP(tm) Project
 12:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 13:  */
 14: 
 15: App::uses('View', 'View');
 16: App::uses('Xml', 'Utility');
 17: 
 18: /**
 19:  * A view class that is used for creating XML responses.
 20:  *
 21:  * By setting the '_serialize' key in your controller, you can specify a view variable
 22:  * that should be serialized to XML and used as the response for the request.
 23:  * This allows you to omit views + layouts, if your just need to emit a single view
 24:  * variable as the XML response.
 25:  *
 26:  * In your controller, you could do the following:
 27:  *
 28:  * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
 29:  *
 30:  * When the view is rendered, the `$posts` view variable will be serialized
 31:  * into XML.
 32:  *
 33:  * **Note** The view variable you specify must be compatible with Xml::fromArray().
 34:  *
 35:  * You can also define `'_serialize'` as an array. This will create an additional
 36:  * top level element named `<response>` containing all the named view variables:
 37:  *
 38:  * {{{
 39:  * $this->set(compact('posts', 'users', 'stuff'));
 40:  * $this->set('_serialize', array('posts', 'users'));
 41:  * }}}
 42:  *
 43:  * The above would generate a XML object that looks like:
 44:  *
 45:  * `<response><posts>...</posts><users>...</users></response>`
 46:  *
 47:  * If you don't use the `_serialize` key, you will need a view. You can use extended
 48:  * views to provide layout like functionality.
 49:  *
 50:  * @package       Cake.View
 51:  * @since         CakePHP(tm) v 2.1.0
 52:  */
 53: class XmlView extends View {
 54: 
 55: /**
 56:  * The subdirectory. XML views are always in xml.
 57:  *
 58:  * @var string
 59:  */
 60:     public $subDir = 'xml';
 61: 
 62: /**
 63:  * Constructor
 64:  *
 65:  * @param Controller $controller
 66:  */
 67:     public function __construct(Controller $controller = null) {
 68:         parent::__construct($controller);
 69: 
 70:         if (isset($controller->response) && $controller->response instanceof CakeResponse) {
 71:             $controller->response->type('xml');
 72:         }
 73:     }
 74: 
 75: /**
 76:  * Skip loading helpers if this is a _serialize based view.
 77:  *
 78:  * @return void
 79:  */
 80:     public function loadHelpers() {
 81:         if (isset($this->viewVars['_serialize'])) {
 82:             return;
 83:         }
 84:         parent::loadHelpers();
 85:     }
 86: 
 87: /**
 88:  * Render a XML view.
 89:  *
 90:  * Uses the special '_serialize' parameter to convert a set of
 91:  * view variables into a XML response. Makes generating simple
 92:  * XML responses very easy. You can omit the '_serialize' parameter,
 93:  * and use a normal view + layout as well.
 94:  *
 95:  * @param string $view The view being rendered.
 96:  * @param string $layout The layout being rendered.
 97:  * @return string The rendered view.
 98:  */
 99:     public function render($view = null, $layout = null) {
100:         if (isset($this->viewVars['_serialize'])) {
101:             return $this->_serialize($this->viewVars['_serialize']);
102:         }
103:         if ($view !== false && $this->_getViewFileName($view)) {
104:             return parent::render($view, false);
105:         }
106:     }
107: 
108: /**
109:  * Serialize view vars.
110:  *
111:  * @param array $serialize The viewVars that need to be serialized.
112:  * @return string The serialized data
113:  */
114:     protected function _serialize($serialize) {
115:         $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
116: 
117:         if (is_array($serialize)) {
118:             $data = array($rootNode => array());
119:             foreach ($serialize as $alias => $key) {
120:                 if (is_numeric($alias)) {
121:                     $alias = $key;
122:                 }
123:                 $data[$rootNode][$alias] = $this->viewVars[$key];
124:             }
125:         } else {
126:             $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
127:             if (is_array($data) && Set::numeric(array_keys($data))) {
128:                 $data = array($rootNode => array($serialize => $data));
129:             }
130:         }
131: 
132:         $options = array();
133:         if (Configure::read('debug')) {
134:             $options['pretty'] = true;
135:         }
136: 
137:         return Xml::fromArray($data, $options)->asXML();
138:     }
139: 
140: }
141: 
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