Zend Routing: Route to route ID
-
29-06-2021 - |
Question
Is it possible to use the Routes ID to redirect to within the controller?
For example, I predefine the login and logout URL with the id of login and logout. In my controller I determine the user needs to be logged out, can I redirect them to that route using the routes id?
Bootstrap
$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')));
Controller
return $this->_redirect('login');
Currently the above wouldn't work, Id have to use /login (aka the base URL to the route).
Solution
I had a similar requirement recently too, the way I solved it was to use the router to assemble the url, and then perform the redirect.
$redirectUrl = Zend_Controller_Front::getInstance()->getRouter()->assemble($userParams, $routeName);
$this->_redirect($redirectUrl);
See Zend_Controller_Router_Interface::assemble
OTHER TIPS
From 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+'
)
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow