在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 on 2010-03-04 02:57  最爱MiKu酱  阅读(232)  评论(0)    收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示