Home > Backend Development > PHP Tutorial > Zend Framework application content to xml plug-in_PHP tutorial

Zend Framework application content to xml plug-in_PHP tutorial

WBOY
Release: 2016-07-21 14:54:42
Original
889 people have browsed it

In this blog post, Thijs Feryn implements the hook method of Zend_Controller_Plugin_Abstract to convert the returned content response into a specific XML format

address without using the context switching controller assistant in the original program. : http://blog.feryn.eu/2009/05/converting-your-zend-framework-mvc-application-into-an-xml-webservice-using-one-single-plugin/

Code : Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] /**
* 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
*/
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() generates a specific xml structure
_setResponse() sets the xml content of the response
_serialize() serializes the returned object into xml (using recursion)
preDispatch() determines whether there is an exception , if there is one, just set the exception and terminate the script execution
postDispatch() returns the response

Use method
$this->_front->registerPlugin(new My_Plugin_Xml());
--------

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364543.htmlTechArticleIn this blog post, Thijs Feryn implemented the Zend_Controller_Plugin_Abstract hook method without using the context switching controller assistant in the original program. In this case, the returned content will be echoed...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template