如何使用 Yii 框架像 Quora 那样制作 url slug ?

发布于 2024-09-26 04:28:06 字数 182 浏览 1 评论 0原文

使用 Quora 让我想知道他们是如何做这样的 slug 的:quora.com/topics-slugs、quora.com/questions-slug 或 quora.com/usernames-slug。

实际上我正在使用 yii 框架开发一个应用程序,我想要一个像 quora 那样的 slugs?

谢谢大家

Using Quora has made me wonder how they do their slug like thes : quora.com/topics-slugs , quora.com/questions-slug or quora.com/usernames-slug.

Actually i am developing an application with yii framework and i want to have a slugs like quora does?

Thanks guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-10-03 04:28:06

您也可以通过重写规则来做到这一点。在你的配置文件中这样的规则:

'controller/<slug:[\w\-]+>'=>'controller/view',

将采用这样的 URL:

controller/my-slug

它将重定向到控制器中的 actionView() ,并将 slug (“my-slug”) 作为 $_GET 变量传递。使用该重写规则,您现在调用 $_GET['slug'] ,它将从 url 返回“my-slug”。

我有一个“slug”行作为主键,所以我只需在 actionView() 中查询数据库中的 $_GET['slug'] ,然后根据 URL 获得正确的记录。就像魅力一样。祝你好运!

更新
除了使用 slug 之外,要摆脱控制器前缀,您可能需要一张大表来跟踪所有 url slug(以防止重复)。如果你有,那么你可以做一些不同的事情:

1 覆盖 onBeginRequest< /a> 在 master slug 表上进行查找以找出要调用的控制器。
2. 对 SiteController 中的单个 ActionIndex 使用主重写,并在该操作中查找主表中的 slug 以找出将用户发送到哪个控制器/操作。重写规则看起来像这样:

'<slug:[\w\-]+>'=>'site/index',

You can also do this with rewrite rules. A rule like this in your config file:

'controller/<slug:[\w\-]+>'=>'controller/view',

will take a URL like this:

controller/my-slug

and it will redirect to the actionView() in your Controller, and pass the slug ("my-slug") in as a $_GET variable. With that rewrite rule you now call $_GET['slug'] and it will return "my-slug" from the url.

I have a "slug" row as a primary key so then I just query the DB for $_GET['slug'] in my actionView() and I get the correct record based on the URL. Works like a charm. Good luck!

UPDATE
To get rid of the controller prefix in addition to using a slug, you will probably need one large table to keep track of all url slugs (to prevent duplicates). If you have that, then you could do a couple of different things:

1 Override onBeginRequest to do a lookup on the master slug table to figure out which Controller to call.
2. Use a master rewrite to a single ActionIndex in the SiteController, and in that action look up the slug in the master table to figure out which controller/action to send the user to. The rewrite rule would look something like this:

'<slug:[\w\-]+>'=>'site/index',
二手情话 2024-10-03 04:28:06
  1. 处理您想要使用的任何特殊字符(例如变音符号)
  2. 删除留下的所有非字母数字字符
  3. 处理空格

像这样的例子:

function _getSlugFromName($name){
   return preg_replace('#[\s]+#','-',preg_replace('#[^\d\w -]*#','',str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),html_entity_decode(mb_strtolower(trim($name),'UTF-8'),ENT_COMPAT,'UTF-8'))));
}
  1. Handle any special characters you wanna use (Umlauts for example)
  2. Remove any non-alpha-numeric-characters that are left
  3. handle white spaces

Something like that for example:

function _getSlugFromName($name){
   return preg_replace('#[\s]+#','-',preg_replace('#[^\d\w -]*#','',str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),html_entity_decode(mb_strtolower(trim($name),'UTF-8'),ENT_COMPAT,'UTF-8'))));
}
冷心人i 2024-10-03 04:28:06

这可能对您有帮助: dburlmanager

提供基于数据库的动态 URL 规则。
基于数据库的动态 URL 规则(漂亮的永久链接或友好的 URL)

这些动态规则就像 Wordpress 的“漂亮的永久链接”或“友好的 URL”。您不必在 URL 上包含控制器名称(或 ID):此扩展可以处理请求 URI 并将其路由到正确的控制器。

This may help you: dburlmanager

Provides dynamic database-based URL rules.
Dynamic URL rules based on database (pretty permalinks or friendly URLs)

These dynamic rules are like Wordpress' "pretty permalinks" or "friendly URLs". You do not have to have the controller name (or ID) on the URL: this extension can handle the request URI and route it to the correct controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文