简体   繁体   English

在给定条件的情况下,如何在CodeIgniter中构造控制器

[英]How do I structure my Controllers in CodeIgniter given the conditions

I am trying to build a web API with CodeIgniter as PHP framework. 我正在尝试使用CodeIgniter作为PHP框架来构建Web API。

This is not a REST api. 这不是REST API。 It is just a Web api which mean all queries will be POST queries. 它只是一个Web api,这意味着所有查询都是POST查询。

A typical query will look like this 典型的查询如下所示

http://host/api1/user/get_name

and

http://host/api2/user/get_name

Under such circumstances api1 and api2 become controllers and user becomes a method under it. 在这种情况下,api1和api2成为控制器,而用户成为其下的方法。

I am not happy with this because then my api1 and api2 classes become too large and also I dont like to mix unrelated code in a single class. 我对此不满意,因为这样我的api1和api2类太大了,而且我不喜欢在单个类中混合不相关的代码。

What can I do ? 我能做什么 ? Can I play with my .htaccess file or the routing configuration such that everytime the server receives a request of the form \\^api\\ it forwards it to appropriate controller ? 我可以使用.htaccess文件或路由配置进行播放,以便每次服务器收到\\ ^ api \\形式的请求时,都会将其转发到适当的控制器吗?

Please note that api1 and api2 are not the only controllers in my system. 请注意,api1和api2不是我系统中唯一的控制器。

You have to consider the couple of things 你必须考虑几件事

  1. Are they interdependent? 他们是相互依存的吗?
  2. You must have some specific reason for maintaining 2 web APIs. 您必须有维护2个Web API的特定原因。

If they are not interdependent then i will recommend you to do this 如果它们不是相互依存的,那么我将建议您这样做

http://host1/user/get_name
http://host2/user/get_name

If they are interdependent then you must do 如果它们是相互依存的,那么您必须做

http://host/api1/user/get_name
http://host/api2/user/get_name

Reason behind that is you do not need to maintain the 2 different models. 其背后的原因是您不需要维护两种不同的模型。

For example, if you made some changes in a X_model.php , you have to upload that on both server. 例如,如果您在X_model.php进行了一些更改,则必须将其上载到两个服务器上。

And you can create folder under controller. 您可以在控制器下创建文件夹。 I am very confirm about that. 我对此非常确认。 I have done it in codeigniter 1.7 . 我已经在codeigniter 1.7做到了。

So you can create folder respectively and continue with it.! 因此,您可以分别创建文件夹并继续。

You could try $route['api(:num)'] = "api$1/"; 您可以尝试$route['api(:num)'] = "api$1/";

Haven't tested dunno if it's gonna work the way you want it. 没有测试过dunno是否可以按照您想要的方式工作。

Codeigniter Routing 代码点火器路由

What are API1 and API2? 什么是API1和API2? Are they independent of each other? 他们彼此独立吗?

If so, I would recommend you use separate subdomains for the two APIs, so you can do this: 如果是这样,我建议您为两个API使用单独的子域,因此您可以这样做:

http://api1.example.com/user/get_name
http://api2.example.com/user/get_name

This provides a clear separation between the two APIs, and allows User to be the controller as opposed to the entire API. 这提供了两个API之间的明确分隔,并允许User成为整个API的控制者。

This would require you to create two directories ( api1, api2 ) in your hosting root, 'install' two instances of CI and route the subdomains to the respective directories. 这将需要您在托管根目录中创建两个目录( api1, api2 ),“安装”两个CI实例,并将子域路由到相应目录。

i see nothing wrong with your url scheme. 我认为您的网址方案没有问题。 this is how you are supposed to call controller -> action in codeigniter. 这就是您应该如何在codeigniter中调用controller-> action的方式。 If you want your controllers to be skinny move the actual processing code in libraries and call the libraries for each request. 如果您希望控制器变瘦,请在库中移动实际的处理代码,并为每个请求调用库。 Or you could play with the rewrite rules to distribute the work across different controllers. 或者,您可以使用重写规则来在不同的控制器之间分配工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM