在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,表示是否重置当前请求变量
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步