Plug-in for converting ZendFramework application content to xml. In this blog post, ThijsFeryn implements the hook method of Zend_Controller_Plugin_Abstract without using the contextswitching Controller assistant in the original program, in this blog post, Thijs Feryn implements the hook method of Zend_Controller_Plugin_Abstract. if the context switching controller assistant is not used in the original program, convert the returned content response to a specific XML format
Address: http://blog.feryn.eu/2009/05/converting-your-zend-framework-mvc-application-into-an-xml-webservice-using-one-single-plugin/
Code:
Copy to ClipboardReference: [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 ){
$ Predictionnode-> appendChild (
$ This-> _ xml-> createElement ('message ',
$ Exception-> getMessage ()
)
);
$ Predictionnode-> appendChild (
$ This-> _ xml-> createElement ('code ',
$ Exception-> getCode ()
)
);
$ Predictionnode-> appendChild (
$ This-> _ xml-> createElement ('type ',
Get_class ($ exception)
)
);
}
$ ResponseNode-> appendChild ($ predictionnode );
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 (): Set the xml content of the response.
_ Serialize () serialize the returned object to xml (recursive)
PreDispatch () determines whether an exception exists. If yes, only the exception is set and script execution is terminated.
PostDispatch () returns a response
Usage
$ This-> _ front-> registerPlugin (new My_Plugin_Xml ());
--------
Feryn implements the hook method of Zend_Controller_Plugin_Abstract. when the context switching controller assistant is not used in the original program, the returned content will ring...