在 CodeIgniter 中进行 URI 路由的最佳方法?
因此,这里有一个 CodeIgniter 网站 Forrst 上的示例:
http://forrst.com/posts/PHP_Nano_Framework_WIP_Just_ throwing_some_ideas_o- mU8
看看那个漂亮的网址。您已经有了根站点,然后是帖子,然后是帖子标题和简短的摘录。这对于用户体验来说非常酷。
然而,我的 CodeIgniter 站点的 URL 实在是太糟糕了。 EG
http://mysite.com/code/view/120
因此它访问控制器< code>code,然后是函数 view
,最后的 20
就是帖子 ID(并据此进行数据库查询)。
我意识到我可以做一些路由。所以在我的routes.php文件中,我输入了以下内容:
$route['posts/(:num)'] = "code/view/$1";
- 所以这将使 http://mysite.com/posts/120 与 http://mysite.com/code/view/120。更好一点,我想你会同意的。
我的问题是 - 我如何使用与 Forrst 类似的技术,从而将帖子的摘录实际附加到 URL 中?我真的不明白这怎么可能。 PHP 脚本如何确定应该在数据库中查找什么内容,特别是如果有多个内容具有相同的标题?
谢谢!
杰克
So, here's an example on Forrst, a CodeIgniter website:
http://forrst.com/posts/PHP_Nano_Framework_WIP_Just_throwing_some_ideas_o-mU8
Look at that nice URL. You've got the root site, then posts
, then the post title and a short extract. This is pretty cool for user experience.
However, my CodeIgniter site's URLs just plain suck. E.G.
http://mysite.com/code/view/120
So it accesses the controller code
, then the function view
, then the 20
on the end is the Post ID (and it does the database queries based on that).
I realised I could do some routing. So in my routes.php file, I put the following in:
$route['posts/(:num)'] = "code/view/$1";
- so this will make http://mysite.com/posts/120 be the same as http://mysite.com/code/view/120. A bit nicer, I think you'll agree.
My question is - how can I use a similar technique to Forrst, whereby an extract of the post is actually appended to the URL? I can't really see how this would be possible. How can the PHP script determine what it should look up in the database, especially if there are several things with the same title?
Thanks!
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取像示例中那样的 URL,您需要添加路由规则,就像您已经完成的那样
$route['posts/(:num)'] = "code/view/$1";
。 Forrst 的 url 似乎是“映射”(或类似的东西),我认为 uri 的最后一部分是标识符(o-mU8
看起来像一个散列,但我更喜欢 int id)存储在数据库中,所以如果他查询,他会用破折号(_)分割uri,并获取它的最后一部分,就像在控制器操作中这样:基本上控制器/和标识符之间的字符串是完全无用的,但如果您的目标是更好的 SEO,则不然。
我希望这有帮助
To get a URL like in your example you need to add a routing rule, like you've already done
$route['posts/(:num)'] = "code/view/$1";
. Forrst's url seems to be "mapped" (or something like that), I think the last part of the uri is the identifier (o-mU8
seems like a hash, but I prefer an int id) which is stored in the db, so if he queries, he splits the uri by the ndashes (_), and gets the last part of it, like this within your controller action:Basically the string between the controller/ and the identifier is totally useless, but not if your goal is a better SEO.
I hope this helps
请参阅官方讨论。通常与此相关的术语是“slug”。我自己还没有测试过 CI 论坛的方法,但建议和示例看起来相当不错。
See the official dicussions. The term that is often related to this is "slug". Haven't tested the approach from the CI forums myself, but the suggestions and examples look pretty good.
codeigniter 中的 URL 助手有一个函数调用 url_title()。我自己没有使用过,但我认为这就是您正在寻找的。
The URL helper in codeigniter has a function call url_title(). I haven't used it myself but I think it's what you are looking for.