问题
This url structure is proposed for SEO optimization. So suggesting another structure will not work. Structure proposed is
example.com/<language>/<country>/<province>/<city>/<product>
example.com/en/spain
I wish to point to CountryController
indexAction
, as view of each one is different, and sometimes think there is change in layout also.
Show the content in English language about the country Spain. And for a request example.com/en/india
should show about India in English language and example.com/es/spain
should show the spanish page for the country Spain.
example.com/en/spain/barcelona
Points to CountryController
provinceAction
Content Page for the Barcelona province of Spain in English Language .
example.com/en/spain/barcelona/barcelona
Points to CountryController
cityAction
Content Page for the Barcelona city in Barcelona province of country Spain in English Language .
example.com/en/spain/barcelona/barcelona/taxis
Points to CountryController
productAction
Content Page for the product in Barcelona city , Barcelona province of country Spain in English Language .
Yes we can add a route like
$router = $ctrl->getRouter();
$router->addRoute(
'country_spain',
new Zend_Controller_Router_Route('spain',
array('controller' => 'country',
'action' => 'index'))
);
But in this case we need to add the whole list of countries to the route. ie india , china , pakistan , unitedstates etc .
Then will be adding country_province
$router = $ctrl->getRouter();
$router->addRoute(
'country_spain_barcelona',
new Zend_Controller_Router_Route('spain',
array('controller' => 'country',
'action' => 'province'))
);
So if we have 50 province it will be horrible to add count of countries multiplied by count of provinces for the countries , and this will become more routes when moving to city and products.
You may say add something like
$router = $ctrl->getRouter();
$router->addRoute(
'country',
new Zend_Controller_Router_Route('country/:country/:province/:city/:product',
array('controller' => 'country',
'action' => 'index'))
);
But in this case its like we will be pointing to same action, but as the view of the requests change, and this will become a fat controller .
The problem with Zend_Controller_Router_Route_Regex is we should have something like
example.com/en/country/spain/barcelona/barcelona/taxis
and also the all moves to a single action. As the view is entirely different, it becomes dirty. May be something like partials I can use. But I wonder whether there is another good solution for the problem to solve this. This is a legacy project, so I have limitations on it and #ZF version is 1.6.
There is an example something similar
http://www.travelportal.info/ http://www.travelportal.info/asia http://www.travelportal.info/asia/india http://www.travelportal.info/asia/india/business-currency-economy
How do you think, they have done this, will they have added routes atleast for asia , europe like that ?
I was able to make it work like
example.com/en/spain
Pointing to CountryController
indexAction
example.com/en/spain/barcelona
Pointing to CountryController
provinceAction
example.com/en/spain/barcelona/barcelona
Pointing to CountryController
cityAction
example.com/en/spain/barcelona/barcelona/taxis
Pointing to CountryController
productAction
But there I need to add 4 routes and this will become hard to manually add the route like this.
Suggestions and criticisms are welcome to make it better.
回答1:
It seems like you want a separate route for each of your example scenarios, e.g.:
$router->addRoute(
'product',
new Zend_Controller_Router_Route(':lang/:country/:province/:city/:product', array(
'controller' => 'country',
'action' => 'product'
))
);
$router->addRoute(
'city',
new Zend_Controller_Router_Route(':lang/:country/:province/:city', array(
'controller' => 'country',
'action' => 'city'
))
);
$router->addRoute(
'province',
new Zend_Controller_Router_Route(':lang/:country/:province', array(
'controller' => 'country',
'action' => 'province'
))
);
$router->addRoute(
'country',
new Zend_Controller_Router_Route(':lang/:country', array(
'controller' => 'country',
'action' => 'index'
))
);
来源:https://stackoverflow.com/questions/9127873/routing-for-country-province-city-product-in-zend-framework