在这一篇博文中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/
代码:
/**
* 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());
——–
波比源码 » Zend Framework应用程序内容转换为xml的插件
levofloxacin 500mg uk levofloxacin 500mg generic
cheap dutasteride buy ondansetron generic purchase ondansetron
buy aldactone 25mg without prescription finasteride 5mg canada diflucan generic
ampicillin 250mg for sale order erythromycin pill erythromycin 250mg canada
purchase fildena pill sildenafil 100mg sale oral robaxin
order suhagra pills sildenafil us estradiol cheap
order lamictal sale order generic retin purchase retin generic
buy tadalafil 20mg generic order avanafil 200mg pill diclofenac 50mg brand
buy accutane 40mg online buy accutane 40mg online cheap buy generic zithromax
purchase indocin generic lamisil over the counter purchase trimox pills
order tadalafil 40mg Sample viagra pfizer viagra
anastrozole 1mg usa cheap anastrozole 1mg sildenafil pill
deltasone 10mg generic cialis 40 mg oral sildenafil
tadalafil 10mg ohne rezept viagra kaufen ohne rezept sildenafil 100mg bestellen
order accutane 20mg without prescription isotretinoin 10mg us ivermectin 0.5%
oral doxycycline 100mg order doxycycline 200mg online lasix 40mg tablet
order altace 10mg online cheap clobetasol price buy astelin 10 ml sprayers
clonidine medication buy meclizine pill buy spiriva 9 mcg online
buy alendronate 35mg generic order fosamax pill order famotidine 40mg pill
benicar 20mg generic buy divalproex 500mg for sale buy acetazolamide pill
generic imdur 40mg buy imdur 20mg online buy telmisartan pill
zyban 150 mg ca order strattera for sale order seroquel 50mg without prescription
order molnunat 200mg online cheap prevacid 30mg brand prevacid 15mg for sale
order zoloft for sale Buy viagra in canada sildenafil 50mg uk
salbutamol 100mcg over the counter over the counter viagra cheap viagra generic
buy cialis for sale order cialis 5mg pills order symmetrel 100 mg pills
order avlosulfon 100mg generic order adalat 30mg generic aceon brand
provera 10mg oral purchase medroxyprogesterone sale periactin 4mg ca
cheap modafinil buy stromectol 3mg online ivermectin 1%
fluvoxamine uk cymbalta price buy glipizide online cheap
order isotretinoin 40mg online buy amoxil 500mg pill prednisone 40mg ca
order piracetam for sale buy piracetam 800mg 50mg viagra
zithromax 500mg drug order zithromax 500mg generic gabapentin 600mg over the counter
order cialis 40mg pill buy cialis 5mg pills sildenafil pills 50mg
buy chloroquine pills cenforce over the counter baricitinib 2mg usa
itraconazole over the counter generic tinidazole purchase tinidazole generic
amlodipine 10mg pill oral cialis 5mg buy tadalafil 10mg
viagra for sale buy viagra sale lisinopril over the counter
order prilosec money slots slots meaning
purchase aristocort pills loratadine sale buy clarinex 5mg pill
dapoxetine 30mg oral dapoxetine 90mg sale purchase synthroid sale
generic cialis cost buy viagra 50mg for sale brand viagra 100mg
order generic orlistat 60mg diltiazem 180mg pills acyclovir tablet
buy methotrexate 2.5mg without prescription order reglan 10mg pill reglan online
cozaar over the counter topiramate 200mg generic buy generic topiramate 100mg
online casino slots no download real online blackjack play poker online for real money
diflucan 100mg cheap brand fluconazole 200mg buy generic viagra
purchase cialis online cheap cialis overnight shipping buy viagra 50mg pills
buy generic ceftin robaxin ca order robaxin 500mg
buy sildalis without prescription sildenafil otc order lamictal 200mg pill
azithromycin cheap buy neurontin 800mg pill neurontin 800mg cheap
buy lasix 40mg online cheap furosemide 100mg us purchase hydroxychloroquine online
prednisone generic prazosin canada buy vermox 100mg
buy sildenafil 50mg generic purchase nolvadex online cheap budesonide spray
retin gel drug avana 100mg canada order avanafil generic
buy tadacip 20mg generic indocin uk indomethacin 50mg generic
lamisil over the counter buy cefixime pill order amoxicillin online
generic clarithromycin 250mg order meclizine 25 mg generic order generic meclizine
buy spiriva 9mcg pills purchase tiotropium bromide online cheap buy hytrin 5mg online cheap
cost actos 15mg real viagra pharmacy prescription viagra for sale online
cialis pills online gambling slots games online casino roulette
tadalafil 10mg uk ed pills comparison buy cialis 10mg generic
cost of stromectol stromectol 3mg uk avlosulfon for sale
play online roulette for fun online blackjack with real money free roulette online
buy generic nifedipine 10mg allegra pills fexofenadine price
essays buy paper assistance buy azulfidine 500mg pill
doxycycline 200mg canada order cleocin for sale order cleocin 150mg generic
mesalamine 800mg over the counter order mesalamine online order avapro pill
order generic acetazolamide 250 mg order acetazolamide pills imuran 50mg for sale
order clobetasol generic generic temovate cordarone 100mg canada
purchase lanoxin generic order molnupiravir pills molnunat 200 mg uk
amoxicillin buy online buy amoxil 250mg sale stromectol over the counter
carvedilol pills buy oxybutynin 2.5mg for sale elavil drug
cheap requip 1mg requip 1mg drug order labetalol 100mg online cheap
tricor 160mg uk viagra sildenafil 150mg viagra 150mg for sale
nexium capsules order biaxin 250mg furosemide 40mg for sale
buy tadalafil 20mg pill order sildenafil pill viagra 100mg tablet
tadalafil 5 mg tablet buy ed medications online buy ed pills online
glucophage 1000mg usa glucophage usa buy nolvadex 20mg online
clomid us buy prednisolone 20mg online cheap purchase prednisolone without prescription
deltasone 40mg canada order isotretinoin sale amoxicillin price
cheap erectile dysfunction cheap lyrica 150mg proscar 1mg for sale
zofran 8mg brand generic zofran 4mg bactrim 480mg for sale
ventolin inhalator price order levothroid online cheap order generic augmentin 625mg
provigil ca provigil 200mg canada lopressor pills
vibra-tabs for sale acyclovir pills zovirax medication
dutasteride tablet order orlistat 120mg pills xenical order online
inderal 20mg cheap order inderal 10mg online cheap oral coreg
buy azathioprine 50mg sale naproxen 500mg tablet buy naproxen 250mg without prescription
ditropan 5mg brand oxcarbazepine 600mg brand purchase oxcarbazepine pill
order cefdinir 300mg generic oral lansoprazole protonix 40mg brand
cost avlosulfon 100mg order generic atenolol order atenolol 50mg
female viagra sildenafil purchasing viagra on the internet buying cialis cheap
phenergan order 10mg cialis buy tadalafil 10mg
levofloxacin 500mg generic buy zyban 150 mg order bupropion 150 mg generic
real cialis discount tadalafil buy ed meds online
tadalafil uk buy cialis overnight delivery where to buy over the counter ed pills
ivermectin lotion for lice deltasone 20mg over the counter isotretinoin 10mg generic
amoxil uk amoxicillin oral order prednisolone without prescription
order isotretinoin 40mg generic amoxil drug order generic azithromycin 250mg
order tenormin 100mg pill oral femara 2.5mg order letrozole without prescription
purchase levothyroxine pill levitra uk buy generic vardenafil
buy albenza 400mg sale buy provera 5mg online order medroxyprogesterone 5mg online cheap
praziquantel 600 mg drug buy praziquantel 600mg generic cyproheptadine 4 mg generic
buy lyrica 75mg sale pregabalin 75mg tablet buy priligy 30mg for sale
order methotrexate pill metoclopramide 20mg pill order reglan 20mg
xenical 120mg pills buy zovirax 800mg generic zyloprim 100mg price
cozaar over the counter where to buy hyzaar without a prescription order topamax 100mg
rosuvastatin 10mg generic purchase crestor generic generic domperidone
buy sumatriptan 25mg online levofloxacin oral dutasteride cheap
cheap plavix 150mg nizoral 200 mg sale order ketoconazole sale
purchase flomax pill order tamsulosin 0.4mg pill aldactone online buy
duloxetine 40mg price order piracetam 800 mg without prescription piracetam 800mg for sale
brand ipratropium 100mcg dexamethasone online buy buy linezolid medication
order prometrium 200mg online cheap progesterone pills buy olanzapine 10mg generic
buy nebivolol 5mg generic valsartan tablet buy generic clozaril online
order simvastatin generic order valtrex 500mg for sale viagra 100mg over the counter
tegretol pill buy generic lincocin 500mg order lincomycin 500mg without prescription
estradiol cost order estradiol 1mg pill minipress 1mg canada
vermox over the counter mebendazole 100mg over the counter tadalafil canada
indomethacin 75mg oral buy indocin 50mg generic generic suprax 200mg
order generic minocycline 100mg hytrin 5mg ca actos pills
cost furosemide 100mg order lasix 100mg pill buy albuterol 2mg pills
altace 10mg brand order amaryl 1mg pills etoricoxib 60mg brand
order vardenafil 10mg online cheap buy generic plaquenil 200mg plaquenil 400mg drug
order mesalamine 400mg online cheap order avapro pill order avapro 300mg for sale
clobetasol over the counter buy cordarone 200mg without prescription purchase amiodarone generic