Codeigniter:将与控制器名称不匹配的所有请求发送到默认控制器 [英] Codeigniter: send all requests that do not match a controller's name to the default controller

查看:210
本文介绍了Codeigniter:将与控制器名称不匹配的所有请求发送到默认控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个涉及两种网址的项目,一个遵循标准的CI模式

  http:// fancysite.com/controller/ 

另一个介绍以下计划:

  http://fancysite.com/category 

我想第二个调用默认控制器的 handlecategory (或类似的)函数与作为参数。



奖金爱,如果你也可以告诉我如何让URL像 http://place.fancysite.com/ 调用相同的函数传递放置作为参数(如果没有类别跟在该网址之后) 放置类别



:我已经知道所有控制器,地点,类别的名称。

解决方案

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



在您的路线文件

  $ route [':any'] ='默认控制器的路径' 

所有其他路由都应放置在代码上方。 p>

假设遇到的网址代码符是 -



http://fancysite.com/category



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

  $ route ['category /:any'] ='实际路径'; 

如果没有指定这样的路由,codeigniter会将这个
请求传递给path



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



现在,您可以在默认控制器中插入子域处理逻辑或任何



您甚至可以在我们的网址范例中取得参数'category',即
在网址中使用codeigniters URI 帮助类如下 -

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


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.

解决方案

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);

这篇关于Codeigniter:将与控制器名称不匹配的所有请求发送到默认控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆