yii重写带有许多子类别的url [英] yii rewrite url with many sub categories

查看:64
本文介绍了yii重写带有许多子类别的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在yii中工作时遇到问题: 我想要这样的网址:

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=....

它指向控制器:CatalogController,动作索引

It point to controller: CatalogController, action Index

URL只是说cat1是父级,cat2是cat3的父级.

The url just say cat1 is parent, cat2 is parent of cat3.

因此,在此操作中,它仅获得最后一个类别的产品(例如,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.

错误:

系统无法找到请求的操作'cat1'"

"The system is unable to find the requested action 'cat1'"

我们必须使用/来分隔每个类别.

We must use / for separating each category.

推荐答案

您需要清除一些内容,此类别列表是否固定(u始终准确地具有3个级别的类别,不少于或多于3个)或类别列表将是动态的. 如果您有固定的类别列表,则可以像这样轻松设置Yii url规则:

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'

或更具动态性:

'<controller:\w+>/<cat1:\w+>/<cat2:\w+>/<cat3:\w+>' => '<controller>/list'

在您希望捕获动态类别列表的情况下,我和我的团队通常使用我们称为SeoBehavior的名称.我们将此行为附加到表示类别内容的模型上,并在保存此行为的基础上根据内容所属的类别列表构建url路径,并将其保存到seo数据库表中(我们保留有关模型名称和模型ID的信息,其他一些有用的配置参数). 比您的网址规则中,您可以添加以下内容:

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'

只有您需要将此规则保持在列表的最后,因为它将捕获所有内容.在此操作中,您将获得"url"参数并搜索seo数据库表以获取记录,然后从该记录中您可以看到什么是模型名称及其ID,以便您可以加载正确的内容.

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.

我希望有帮助.

这篇关于yii重写带有许多子类别的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆