how to use Zend Route Regex to route a url to another one?
-
03-07-2021 - |
Question
i have a url like this: view_test.php?user=56
and i am trying to get it here:
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'test/view/:id',
array(
'module' => 'test',
'controller' => 'index',
'action' => 'view'
)
)
);
basically from view_test.php?user=56
to /test/view/56
im not sure how to handle the ?
.
any ideas?
thanks
Solution
AFAIK, in a route, you can't reference the url's query-string content, just its request path. But you could do the following:
Add a route that maps the old url to a redirect action:
$router->addRoute('view-test-redirect', new Zend_Controller_Router_Route_Static(
'view_test.php',
array(
'module' => 'test',
'controller' => 'index',
'action' => 'view-test-redirect',
)
);
Also, add a route representing your "real" action:
$router->addRoute('view-test', new Zend_Controller_Router_Route(
'view/test/:user',
array(
'module' => 'test',
'controller' => 'index',
'action' => 'view-test',
)
);
The action name view-test-redirect
corresponds to the action below:
public function viewTestRedirectAction()
{
$user = (int) $this->_getParam('user', null);
if (!$user){
throw new \Exception('Missing user');
}
$this->_helper->redirector->goToRouteAndExit(array('user' => $user), 'view-test');
}
Then your view-test
action can do things in the expected way:
public function viewTestController()
{
$user = (int) $this->_getParam('user', null);
if (!$user){
throw new \Exception('Missing user');
}
// Read db, assign results to view, praise unicorns, etc.
}
Not tested, just brain-dumping to demonstrate the idea.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow