Codeigniter _remap函数

发布于 2024-09-16 14:55:19 字数 371 浏览 4 评论 0原文

请帮助我想在我的 CodeIgniter 网站中使用第一个 URI 段。

就像当我打开这些网址时,他们会打开我的个人资料: http://www.facebook.com/buddyforever 或者 http://www.myspace.com/zarpio

如何使用 CodeIgniter 执行此操作?我检查了 _remap 函数,但先来的控制器如何隐藏控制器?

Please help I want to use first URI segment into my CodeIgniter website.

Like when I open these url they opens my profile:
http://www.facebook.com/buddyforever
or
http://www.myspace.com/zarpio

How can I do this with CodeIgniter? I checked _remap function but first coming controller how to hide controller?

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

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

发布评论

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

评论(1

久伴你 2024-09-23 14:55:19

您可以使用 codeigniter 的 URL 路由来完成此操作...

如果您希望 URL 为 http://www.mydomain.com/zarpio 并且希望它引用 your_controller< /code>,然后执行以下操作。

/config/routes.php

$route['(.*)'] = "your_controller/$1"; // Now, `zarpio` will be passed to `your_controller`

您可以像这样在控制器中访问它...

$my_name = $this->uri->rsegment(2);

但是我不建议这种配置 URL 的方式。更好的方法是...

$route['users/(.*)'] = "your_controller/$1";

这样,您只需将控制器名称 your_controller 重命名为 users 即可。

如果您想访问用户的个人资料,您可以这样做......

$route['users/profile/(.*)'] = "another_controller/method/$1";
$route['users/(.*)'] = "your_controller/$1";

考虑路由的顺序。由于您在路由中写入了 users/(.*) ,因此它将匹配 users/zarpio 以及 users/profile/zarpio,并且将它们都路由到 your_controller/$1,在配置文件的情况下会给您一个 404 page not found 错误。这就是为什么您需要在路由配置中的 users/(.*) 之前编写 users/profile/(.*)

更多信息请参见 codeigniter URI 类文档

You can do this using the URL routing of codeigniter...

If you want your URL to be http://www.mydomain.com/zarpio and you want it to refer to your_controller, then do the following.

/config/routes.php

$route['(.*)'] = "your_controller/$1"; // Now, `zarpio` will be passed to `your_controller`

You can access it in your controller like this...

$my_name = $this->uri->rsegment(2);

However I do not suggest this way of configuring URLs. A better way would be...

$route['users/(.*)'] = "your_controller/$1";

This way, you're just renaming your controller name your_controller to users.

If you want to access profile of a user, you can do it like this...

$route['users/profile/(.*)'] = "another_controller/method/$1";
$route['users/(.*)'] = "your_controller/$1";

Consider the order of routing. Since you wrote users/(.*) in your route, it will match users/zarpio as well as users/profile/zarpio, and route both of them to your_controller/$1, which in the case of profile will give you a 404 page not found error. That is why you need to write users/profile/(.*) before users/(.*) in your routing configuration.

More information in codeigniter URI class documentation

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