dty98339 2011-10-13 07:33
浏览 23
已采纳

codeigniter PHP中的MVC设计

When should a new model or controller be made? Should there only be controllers that go with a corresponding view 1 to 1 and like so with controllers and models? Or is it good practice to have controllers for functionality not tied up with any particular view? Take for example voting, should all voting code go in a voting controller or be spread among controllers that have views using voting. Seems likely a voting controller would be best.

  • 写回答

3条回答 默认 最新

  • dongzhentiao2326 2011-10-13 09:04
    关注

    First of all , you cannot actually implement classical MVC in php. The best you can do is Model2 MVC.

    • Model - responsible for all the business logic. Has no clue about where the data is stored or actually comes from. The storage and retrieval is responsibility of DataMappers or DAOs. Model itself should never contain SQL. Ever.
    • Controller - binds model(s) to the view and changes the state of both. It does not retrieve information from models for sending it to view.
    • View - responsible for all presentational logic. Retrieves information from models and binds it to appropriate templates. View itself is not a template.

    You can wither have 1:1 relationship between views an controller or many:many. it depends on how you implement the view itself.

    For example, your view can require a separate object which handles the rendering. And providing different type of objects ( this is where polymorphism matters ), you can make your view to render either xml, html or json.

    Or you can do the same by changing the templates. A ListView class can render a list of articles as well as list of users, if the basic presentation logic does not change and you provide just a different template for each.

    In the case of voting , it seems ok to have a single controller for all voting related operations, and you can have a single view with switching the templates for your output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口