Change route in Zend Framework to hide key
-
08-07-2021 - |
Question
I am building a website with Zend Framework but I'm very new at it and still don't really get it.
I have a URL like:
http://localhost/public/directory/category/catid/art
What I want is to create a route for the URLto look like:
http://localhost/public/directory/category/art
I have done simple routes like:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
// TERMNS ROUTE
$route = new Zend_Controller_Router_Route_Static(
'termns',
array('controller' => 'Index', 'action' => 'termns')
);
$router->addRoute('terms', $route);
Is there a way, building on that for me to control the other routes? Thanks!
Solution
From category/catid/art
to category/art
.
Add this method to your Bootstrap.php:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('category/:catid', new Zend_Controller_Router_Route('category/:catid',array(
'module' => 'default',
'controller' => 'category',
'action' => 'index' // Check your action and controller
));
And in controller's action for category/testid
you can use $this->_getParam('catid')
for getting testid
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow