2010年3月4日

在zend_form中设置路由Router

设置好路由之后怎么使用呢?在控制器中有redirection 控制器助手,在视图中有url视图助手

Naneau在这篇博文中使用$route->assemble()设置表单的url

地址:http://naneau.nl/2008/06/03/router-abuse/

/**
* set the action based on a route
*
* @param array $params
* @param string $routeName
* @param bool $reset
*/
public function setRouteAction($params = array(), $routeName = null, $reset = false)
{
$frontController = Zend_Controller_Front::getInstance();
//the FC

$router = $frontController->getRouter();
//the router (assumes rewrite router)

if ($reset) {
$routeName = 'default';
}
//hmm... this may lead to unexpected results
//but so will getting the current route name


if ($routeName == null) {
try {
$routeName = $router->getCurrentRouteName();
}
catch (Zend_Controller_Router_Exception $e) {
$routeName = 'default';
}
}
//requested routename/default

$route = $router->getRoute($routeName);
//the current route

$action = rtrim($frontController->getBaseUrl(), '/') . '/';
$action .= $route->assemble($params, $reset);

$this->setAction($action);
}

注意getCurrentRouteName(),getRoute(),assemble()方法的使用 

$params = array(
'module' => 'blog',
'controller' => 'post',
'action' => 'read'
'id' => 13
);
$route = $router->getRoute('default');
$url = $route->assemble($params, true);
//create a url using just the params set in $params
//this will look like blog/post/read/id/13

assemble的第2个参数为可选的boolean,表示是否重置当前请求变量

posted @ 2010-03-04 02:57 最爱MiKu酱 阅读(66) 评论(0) 编辑

使用一个插件将Zend Framework应用程序的内容转换为xml

在这一篇博文中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
*/
/**
* 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());

--------
在另一blog中也有涉及,地址暂时忘记了

 

posted @ 2010-03-04 02:56 最爱MiKu酱 阅读(252) 评论(0) 编辑

  
<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

导航

统计

公告

昵称:最爱MiKu酱
园龄:1年11个月
粉丝:0
关注:0

搜索

 
 

常用链接

我的标签

随笔档案

最新评论