简体   繁体   English

如何在Codeigniter中构造控制器?

[英]How to structure controllers in Codeigniter?

I was wandering what is the best way of structuring Codeigniter Controllers. 我在徘徊什么是构造Codeigniter控制器的最佳方法。

Let me explain: as far as I know you should have a corresponding Model and Controller for each 'object' you are representing; 让我解释一下:据我所知,您应该为您代表的每个“对象”都有一个对应的模型和控制器; but there are some people that argue it's better to group actions into website sections. 但是有些人认为将操作分组为网站部分更好。

For example: 例如:

  / /controllers/user.php /controllers/order.php 

As opposed to: 相对于:

  / /controllers/front.php /controllers/admin.php 

If you go with the second structure I thing you'll just end up with long and and messy controllers. 如果您使用第二种结构,那么最终您将得到冗长且混乱的控制器。

So what the arguments and why is one better than the other ? 那么,什么论据以及为什么一个论据优于另一个论据呢?

最好的答案可能是使用HMVC模块将两者结合在一起。

Use Modular Extensions for Codeigniter: http://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview - it will allow you to break your code into reusable modules. 对Codeigniter使用模块化扩展: http ://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview-它可以让您将代码分解为可重用的模块。 Modules are like applications within your application and can have their own libraries, controllers, models and anything else. 模块就像应用程序中的应用程序一样,可以具有自己的库,控制器,模型和其他任何东西。

Once you go modular, you'll never go back to sub folders again which get messy when you have a few of them anyway. 一旦使用了模块化,就永远不会再回到子文件夹了,无论如何,当这些子文件夹很少时,它们会变得混乱。

Why don't you try using subfolders? 为什么不尝试使用子文件夹?

controllers/front/
                  user.php
                  order.php
controllers/admin/
                   //admin controllers

models/front/
              user.php
              order.php
models/admin/
              //admin models

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

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