views:

159

answers:

1

I would like to achieve something like this with ZF router. ('controller' => 'products', 'action' => 'index')

//Show product details   
'http://mydomain.com/en/products/category-1/subcategory-3/product-name.htm'

//List products in subcategory-3
'http://mydomain.com/en/products/category-1/subcategory-3.htm'

//List products in category-1
'http://mydomain.com/en/products/category-1.htm'

//List products in all categories
'http://mydomain.com/en/products.htm'

Do I have to specify each possibility as separate route? Or is there better solution? (Using Route_Regex?)

+1  A: 

You can get a route to approximate that by having empty defaults like so:

resources.router.routes.products.route = ":lang/products/:category/:subcategory/:product"
resources.router.routes.products.defaults.category=""  
resources.router.routes.products.defaults.subcategory=""       
resources.router.routes.products.defaults.product=""
resources.router.routes.products.defaults.controller="products"
resources.router.routes.products.defaults.action="index"

That route isn't going to handle your .htm extension on /en/products.htm, and the category/subcategory/product might have a .htm appended to their values. It is probably easier to handle stripping the .htm from the request with a little .htaccess RewriteRule magic rather than working around it with basic Zend_Router)

 RewriteRule /([^/]+)/products(.*)\.htm /$1/products$2

The alternative would be to use a Custom or Regular Expression route

gnarf
Thanks! The idea of stripping the `.htm` part seems to be an easy and clean solution. Thanks for the link too.
Petr Peller
@gnarf I just found that the solution with mod_rewrite does not work, because the Zend_Router only sees the original URI, not the rewritten. The only way is to use [R]edirect flag, but it has no sense in this case. :-(
Petr Peller