多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > Zend Framework應用程序內容轉換為xml的插件

Zend Framework應用程序內容轉換為xml的插件

來源:程序員人生   發布時間:2013-10-06 00:10:46 閱讀次數:3209次
在這一篇博文中Thijs Feryn通過實現Zend_Controller_Plugin_Abstract的hook方法,在原程序沒有使用context switching控制器助手的情況下,將返回的內容響應轉為為特定的XML格式

地址:http://blog.feryn.eu/2009/05/converting-your-zend-framework-mvc-application-into-an-xml-webservice-using-one-single-plugin/

代碼:
<?php
/**
* My_Plugin_Xml component
* Turns an Zend Framework MVC website into an XML webservice
*  File From Liehuo.Net
*/
/**
* My_Plugin_Xml class
*
* @author Thijs Feryn <thijs@feryn.eu>
*/
class My_Plugin_Xml extends Zend_Controller_Plugin_Abstract
{
/**
* Stores the front controller
*
* @var Zend_Controller_Front
*/
private $_front;
/**
* Stores the XML output in DOMDocument format
*
* @var DOMDocument
*/
private $_xml;
/**
* Class constructor
*/
public function __construct()
{
$this->_front = Zend_Controller_Front::getInstance();
$layout = Zend_Layout::getMvcInstance();
$layout->disableLayout();
}
/**
* Build DOMDocument to convert output to XML
*
* @param mixed $return
* @param Exception $exception
* @return string
*/
private function _getXML($return = null,Exception $exception = null)
{
$this->_xml = new DOMDocument('1.0', 'UTF-8');
$this->_xml->formatOutput = true;

$responseNode = $this->_xml->createElement('response');

$exceptionNode = $this->_xml->createElement('exception');
if(null !== $exception && $exception instanceof Exception ){
$exceptionNode->appendChild(
$this->_xml->createElement('message',
$exception->getMessage()
)
);
$exceptionNode->appendChild(
$this->_xml->createElement('code',
$exception->getCode()
)
);
$exceptionNode->appendChild(
$this->_xml->createElement('type',
get_class($exception)
)
);
}

$responseNode->appendChild($exceptionNode);
if(null !== $return){
$responseNode->appendChild(
$this->_serialize('return',$return)
);
} else {
$responseNode->appendChild(
$this->_xml->createElement('return')
);
}

$this->_xml->appendChild($responseNode);
return $this->_xml->saveXML();
}
/**
* Modify the HTTP response object
* Remove the HTML body, replace with XML and change the content-type
*
* @param mixed $return
* @param Exception $exception
*/
private function _setResponse($return = false,Exception $exception = null)
{
$this->getResponse()->setHeader('Content-Type','text/xml; charset=UTF-8');
$this->getResponse()->clearBody();
$this->getResponse()->setBody(
$this->_getXML($return,$exception)
);
}
/**
* Serialize a mixed value to XML in DOMElement format
* This method can be used recursively in case of objects and arrays
*
* @param string $name
* @param mixed $value
* @return DOMElement
*/
private function _serialize($name,$value)
{
if(is_array($value)){
$element = $this->_xml->createElement($name);
foreach ($value as $k=>$v){
if(is_numeric($k)){
$k = 'item';
}
$element->appendChild($this->_serialize($k,$v));
}
} elseif(is_object($value)){
$element = $this->_xml->createElement($name);
$reflection = new ReflectionObject($value);
$properties = $reflection->getProperties();
foreach ($properties as $property){
if($property->isPublic()){
$element->appendChild(
$this->_serialize(
$property->getName(),
$property->getValue($value)
)
);
}
}
}else{
$element = $this->_xml->createElement(
$name,
(string)$value
);
}
return $element;
}
/**
* preDispatch hook that retrieves if an Exception was thrown in the application
* If an exception is thrown, the exception is passed to the exception part of the XML output and script execution is terminated
*
* @param Zend_Controller_Request_Abstract $request
*/
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if($this->getResponse()->isException()){
$exArray = $this->getResponse()->getException();
$this->_setResponse(null,$exArray[0]);
$this->getResponse()->sendResponse();
exit();
}
}
/**
* postDispatch hook that serializes the view object to XML by modifying the HTTP response
* If no exception was thrown script execution continues and the postDispatch method will be called
*
* @param Zend_Controller_Request_Abstract $request
*/
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
$this->_setResponse($view);
}
}

_getXML()產生特定的xml結構
_setResponse()設置響應的xml內容
_serialize()將返回的對象序列化為xml(使用遞歸)
preDispatch()判斷是否有異常,如果有則只設置異常并終止腳本執行
postDispatch()返回響應

使用方法
$this->_front->registerPlugin(new My_Plugin_Xml());
--------
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
為碼而活
積分:4237
15粉絲
7關注
欄目熱點
關閉
程序員人生
主站蜘蛛池模板: 国产成人亚洲综合在线 | jiujiure精品| 欧美xxxx性特级高清 | 久久视频精品53在线观看 | 欧美一区二区不卡视频 | 欧美在线网站 | 亚洲一二三区在线观看 | 久久久久久综合对白国产 | 亚洲福利 | 亚洲 自拍 另类 欧美 综合 | 91在线亚洲精品一区 | 羞羞视频日本动漫免费网站 | 亚洲春色www | a免费国产一级特黄aa大 | 日韩欧美一及在线播放 | 成人国产永久福利看片 | 亚洲伊人久久大香线蕉啊 | 久草香蕉视频在线观看 | 2018精品国产一区二区 | 尤物福利在线 | 亚洲成在线观看 | 亚洲精品一二三区 | 97av在线播放 | 激情视频网站在线观看 | 欧美一级别 | 日本乱人伦片中文字幕三区 | 亚洲网站在线看 | 性色影院 | 日本一区二区高清 | 亚洲天堂国产精品 | 欧美成人在线观看 | 婷婷色九月综合激情丁香 | 日本96在线精品视频免费观看 | 国产性色强伦免费看视频 | 看一级毛片免费观看视频 | 欧美 国产 小说 另类 | 国产午夜亚洲精品久久www | 一二三四视频社区5在线高清视频 | 欧美一级视频免费 | 国内免费视频成人精品 | 国产精品视频福利 |