问题
I'm working in yii with problem: I want to make an url like this:
http://domainabc.com/catalog/cat1/cat2/cat3....?sort=name&limit=10&brand=....
It point to controller: CatalogController, action Index
The url just say cat1 is parent, cat2 is parent of cat3.
So in this action It just get products of the last category (cat3 for example)
But currently I don't know what's the best way to get the last cat to get products.
Error:
"The system is unable to find the requested action 'cat1'"
We must use /
for separating each category.
回答1:
There is something that u need to clear, is this list of categories fixed (u will always have exactly 3 level of categories, no less or more) or this list of categories will be dynamic. In case where u have fixed list of categories u can easy setup Yii url rule for that like this:
'catalog/<cat1:\w+>/<cat2:\w+>/<cat3:\w+>' => 'catalog/myAction'
or little more dynamic:
'<controller:\w+>/<cat1:\w+>/<cat2:\w+>/<cat3:\w+>' => '<controller>/list'
In case where u wish to have do catch dynamic list of categories, me and my team usually use something what we named SeoBehavior. We attach this behavior to model that represent content of category and on save this behavior build url path based on list of categories where this content belong and save it to seo database table (we keep info about model name and model id and there can be also some other useful config parameters). Than in your url rules u can add something like this:
'<url:[a-z0-9\-_\.\/]+>' => 'myController/myAction'
Only u need to keep this rule like last on list since it will catch everything. In this action than u get this 'url' parameter and search seo database table to get record and from that record u can see what is model name and its id so u can load right content.
I hope this help.
来源:https://stackoverflow.com/questions/24622804/yii-rewrite-url-with-many-sub-categories