With the Zend Framework, I am trying to build a URL to SEO friendly URL
http://ww.website.com/public/index/categories/category/2
should be mapped by http://ww.website.com/public/index/categories/category.html
http://ww.website.com/public/index/products/category/2/subcategory/2
should be mapped by http://ww.website.com/public/index/products/product.html
In my Bootstrap file I have tried this
protected function _initRoutes()
{
$frontController = Zend_Controller_Front::getInstance ();
$router = $frontController->getRouter ();
$route = new Zend_Controller_Router_Route_Regex(
'index/categories/category/(\d*)/',
array('module' => 'default','controller' => 'index','action' => 'categories'),
array(1 => 'category'),
'index/categories/%d-%s.html');
$router->addRoute('category', $route);
}
Now my question is how to rewrite this URL? I am also retrieving the products or categories based on the IDs in the URL using:
$id = $this->_getParam('category', 0 );
Anyone could help me?
I think you do not need Regex route for what you need.
If you want for example this url:
http://mysite.com/category/cars.htmlto go tohttp://mysite.com/categories/category/id/2is enough to use Zend_Controller_Router_RouteNow is only one problem Zend Router will not know id of you category to map, for this you need to store your custom routes to database or a file; Each custom route will contain url that map and required parameters (controller name, action name, id of category).
After we have that we can build a function that register a custom route to bootstrap file, or in some plugin like this one:
Now let suppose that
$params['url']has value:http://mysite.com/category/cars.htmlwhen is requested from browser that url ZF will match our custom route and forward request to specified controller in$params['controller'], and call specified action in$params['action']and will set as parameter named id value from$params['id']wich can be retrieved from controller with$id = $this->_getParam('id',0);