mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
starting to refactor views.
Adding comments. Incomplete.
This commit is contained in:
parent
c64e41eb45
commit
011fd51539
2 changed files with 43 additions and 20 deletions
|
@ -1,9 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* A custom view class that is used for JSON responses
|
|
||||||
*
|
|
||||||
* PHP 5
|
|
||||||
*
|
|
||||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||||
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
*
|
*
|
||||||
|
@ -12,17 +8,31 @@
|
||||||
*
|
*
|
||||||
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
* @link http://cakephp.org CakePHP(tm) Project
|
* @link http://cakephp.org CakePHP(tm) Project
|
||||||
* @package Cake.View
|
|
||||||
* @since CakePHP(tm) v 2.1.0
|
|
||||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
App::uses('View', 'View');
|
App::uses('View', 'View');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JsonView
|
* A view class that is used for JSON responses.
|
||||||
|
*
|
||||||
|
* By setting the 'serialize' key in your controller, you can specify a view variable
|
||||||
|
* that should be serialized to JSON and used as the response for the request.
|
||||||
|
* This allows you to omit views + layouts, if your just need to emit a single view
|
||||||
|
* variable as the JSON response.
|
||||||
|
*
|
||||||
|
* In your controller, you could do the following:
|
||||||
|
*
|
||||||
|
* `$this->set(array('posts' => $posts, 'serialize' => 'posts'));`
|
||||||
|
*
|
||||||
|
* When the view is rendered, the `$posts` view variable will be serialized
|
||||||
|
* into JSON.
|
||||||
|
*
|
||||||
|
* If you don't use the `serialize` key, you will need a view + layout just like a
|
||||||
|
* normal view.
|
||||||
*
|
*
|
||||||
* @package Cake.View
|
* @package Cake.View
|
||||||
|
* @since CakePHP(tm) v 2.1.0
|
||||||
*/
|
*/
|
||||||
class JsonView extends View {
|
class JsonView extends View {
|
||||||
|
|
||||||
|
@ -53,13 +63,14 @@ class JsonView extends View {
|
||||||
* @return string The rendered view.
|
* @return string The rendered view.
|
||||||
*/
|
*/
|
||||||
public function render($view = null, $layout = null) {
|
public function render($view = null, $layout = null) {
|
||||||
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
|
if (isset($this->viewVars['serialize'])) {
|
||||||
$this->_render($viewFileName);
|
$serialize = $this->viewVars['serialize'];
|
||||||
}
|
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
||||||
|
|
||||||
$data = isset($this->viewVars['serialize']) ? $this->viewVars['serialize'] : null;
|
|
||||||
|
|
||||||
return $this->output = json_encode($data);
|
return $this->output = json_encode($data);
|
||||||
}
|
}
|
||||||
|
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
|
||||||
|
return $this->output = $this->_render($viewFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* A custom view class that is used for XML responses
|
|
||||||
*
|
|
||||||
* PHP 5
|
|
||||||
*
|
|
||||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||||
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
*
|
*
|
||||||
|
@ -12,8 +8,6 @@
|
||||||
*
|
*
|
||||||
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||||
* @link http://cakephp.org CakePHP(tm) Project
|
* @link http://cakephp.org CakePHP(tm) Project
|
||||||
* @package Cake.View
|
|
||||||
* @since CakePHP(tm) v 2.1.0
|
|
||||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -21,9 +15,27 @@ App::uses('View', 'View');
|
||||||
App::uses('Xml', 'Utility');
|
App::uses('Xml', 'Utility');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XmlView
|
* A view class that is used for creating XML responses.
|
||||||
|
*
|
||||||
|
* By setting the 'serialize' key in your controller, you can specify a view variable
|
||||||
|
* that should be serialized to XML and used as the response for the request.
|
||||||
|
* This allows you to omit views + layouts, if your just need to emit a single view
|
||||||
|
* variable as the XML response.
|
||||||
|
*
|
||||||
|
* In your controller, you could do the following:
|
||||||
|
*
|
||||||
|
* `$this->set(array('posts' => $posts, 'serialize' => 'posts'));`
|
||||||
|
*
|
||||||
|
* When the view is rendered, the `$posts` view variable will be serialized
|
||||||
|
* into XML.
|
||||||
|
*
|
||||||
|
* **Note** The view variable you specify must be compatible with Xml::fromArray().
|
||||||
|
*
|
||||||
|
* If you don't use the `serialize` key, you will need a view + layout just like a
|
||||||
|
* normal view.
|
||||||
*
|
*
|
||||||
* @package Cake.View
|
* @package Cake.View
|
||||||
|
* @since CakePHP(tm) v 2.1.0
|
||||||
*/
|
*/
|
||||||
class XmlView extends View {
|
class XmlView extends View {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue