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 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输