在 CodeIgniter 中进行 URI 路由的最佳方法?

发布于 2024-09-06 20:56:26 字数 1070 浏览 4 评论 0原文

因此,这里有一个 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/120http://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 技术交流群。

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

发布评论

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

评论(3

荆棘i 2024-09-13 20:56:26

要获取像示例中那样的 URL,您需要添加路由规则,就像您已经完成的那样 $route['posts/(:num)'] = "code/view/$1"; 。 Forrst 的 url 似乎是“映射”(或类似的东西),我认为 uri 的最后一部分是标识符(o-mU8 看起来像一个散列,但我更喜欢 int id)存储在数据库中,所以如果他查询,他会用破折号(_)分割uri,并获取它的最后一部分,就像在控制器操作中这样:

$elements = explode('_',$this-uri-segment(2));
$identifier = $elements[count($elements)-1];

$results = $this->myModel->myQuery($identifier);

基本上控制器/和标识符之间的字符串是完全无用的,但如果您的目标是更好的 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:

$elements = explode('_',$this-uri-segment(2));
$identifier = $elements[count($elements)-1];

$results = $this->myModel->myQuery($identifier);

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

别靠近我心 2024-09-13 20:56:26

请参阅官方讨论。通常与此相关的术语是“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.

爱要勇敢去追 2024-09-13 20:56:26

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.

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