MVC Design - How many controllers can/should/must I have in a CodeIgniter MVC web app project?

前端 未结 2 1471
醉话见心
醉话见心 2020-12-31 08:57

I\'m building a relatively simple web application in PHP using the CodeIgniter MVC framework. I\'ve developed PHP applications before but never in a disciplined manner. I\'m

相关标签:
2条回答
  • 2020-12-31 09:37

    Controllers

    It all depends upon the nature of the application, but in general the answer is NO you should not have "one fairly large controller".

    The more you break an application up into smaller pieces, the easier it is to maintain.

    Models

    Directly from the Codeigniter docs

    Models are PHP classes that are designed to work with information in your database.

    The answer is yes, you should only use models for data interaction.

    I think it's funny you actually answered yourself

    "... or can I put 'helper' functions in there as well, ..."

    It happens Codeigniter has a facility that handles this type of functionality...

    Codeigniter Helpers

    0 讨论(0)
  • 2020-12-31 09:51

    (This is my subjective opinion - it has treated me well)

    Models should be the meat and bone of your entire applications. The models should handle all business logic and database management. Meanwhile, the controllers should be as thin as possible, only really providing an interface between the model and view.

    For instance, in a login screen, the controller should provide the user with the login view. When the user inputs his information, the controller should handle input validation and forward the input to the model, which should respond with "success" or "failure". Consequently the controller should redirect the user to the dashboard, or send him back to the login screen with an error message - respectively.

    To summarize: Models should be fat, controllers should be thin.

    0 讨论(0)
提交回复
热议问题