是否可以在控制器内使用Routes ID重定向到?
例如,我预定义了登录和注销URL,id为login和logout。在我的控制器中,我确定用户需要注销,我可以使用路由id将他们重定向到该路由吗?
引导
$router->addRoute('logout',new Zend_Controller_Router_Route('logout', array('module' => 'user', 'controller' => 'index', 'action' => 'logout')));
$router->addRoute('login', new Zend_Controller_Router_Route('login', array('module' => 'user', 'controller' => 'index', 'action' => 'login')));
控制器
return $this->_redirect('login');
目前,上面的方法不起作用,Id必须使用/login(也就是路由的基本URL)。
我最近也有类似的需求,我解决这个问题的方法是使用路由器组装url,然后执行重定向。
$redirectUrl = Zend_Controller_Front::getInstance()->getRouter()->assemble($userParams, $routeName);
$this->_redirect($redirectUrl);
请参阅Zend_Controller_Router_Interface::组装
来自Zend Framework 1.8
$route = new Zend_Controller_Router_Route(
'index/:ident',
array(
'module' => 'user'
'controller' => 'index',
'action' => 'login'
),
array(
// match only digits
'ident' => ''d+'
)
);