Codeigniter:将所有与控制器名称不匹配的请求发送到默认控制器

发布于 2024-10-09 09:06:09 字数 521 浏览 0 评论 0原文

我正在开发一个涉及两种 URL 的项目,一种遵循标准 CI 模式

http://fancysite.com/controller/

,另一种呈现以下方案:

http://fancysite.com/category

我希望第二个调用默认控制器的 handlecategory (或类似的东西)以 category 作为参数的函数。

如果您还可以告诉我如何让 http://place.fancysite.com/ 等 URL 调用,那就太好了如果该 URL 后面没有类别,则该函数仅传递 place 作为参数;如果有,则同时传递 placecategory

附加数据:我已经知道所有控制器的名称、地点、类别。

I'm working on a project involving two kinds of URLs, one following the standard CI pattern

http://fancysite.com/controller/

And another presenting the following scheme:

http://fancysite.com/category

I would like the second to call the default controller's handlecategory (or something like that) function with category as an argument.

Bonus love if you could also tell me how to let URLs like http://place.fancysite.com/ call the same function passing just place as an argument if no category follows that URL, or both place and category if it does.

Additional datum: I already know the names of all controllers, places, categories.

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

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

发布评论

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

评论(1

↘人皮目录ツ 2024-10-16 09:06:09

您可以使用 codeigniter 的 URI 路由 来实现此目的 -

在末尾插入此内容你的路由文件的 -

$route[':any'] = 'path of the default controller';

所有其他路由都应该放在上面的代码之上。

假设 codeigniter 遇到的 URL 是 -

http://fancysite.com/category

因此,首先 codeigniter 会搜索是否存在名为category 的控制器。
如果没有得到,那么它将检查路由文件以检查是否存在
像这样指定的任何路由 -

$route['category/:any'] = 'actual path';

如果在这种情况下没有指定这样的路由,则 codeigniter 将通过此路由
请求您在最后一行中提到的默认控制器的路径
的路线。

即使链接包含子域,请求也不会转到
相同的默认控制器。

现在您可以插入子域处理或其他任何内容的逻辑
在默认控制器中。

您甚至可以在我们的 url 示例中获取参数'category',即
在 URL 中使用 codeigniters URI 帮助程序类,如下 -

$this->uri->segment(1);

You can use the codeigniter's URI routing for achieving this -

Insert this at the end of your route file -

$route[':any'] = 'path of the default controller';

All the other routes should be placed above the upper code.

Lets say the URL codeigniter encountered is -

http://fancysite.com/category

So firstly codeigniter will search if there is a controller named category.
if it does not get that then it will check the route file to check that is there
any route specified like this -

$route['category/:any'] = 'actual path';

If there is no such route specified in that case codeigniter will pass this
request to path of the default controller you have mentioned in your last line
of routes.

Even if the link contains sub-domain it doesn't matter the request will go to
same default controller.

Now you can insert logic for sub-domain handling or anything else
in the default controller.

You can even obtain the parameter 'category' in our url example taken which is
in the URL using codeigniters URI helper class as below -

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