douxun4860 2011-12-03 16:27
浏览 107

PHP MVC:我真的需要一个控制器吗?

My question is simple but (I guess) hard to answer:

Do I really need a complete Model-View-Controller design pattern in my PHP Website / Web-App?

I can't understand how a Controller could be useful with PHP since every PHP site is generated dynamically on every request. So after a site is generated by PHP, there is no way to let the View (the generated HTML site in the Browser) interact with the controller, since the Controller is on the server side and generated once for each site request, even if the request is AJAX...

Do I understand something completely wrong?

Why should I use any kind of MVC PHP Framework like Zend or Symphony?

EDIT: For example lets assume, there should be a website to represent a list of customers in a table:

My Model would be a simple Class Customer on the server side that queries the database. My View would be the generated HTML code that displays the Model (list of Customers). And the controller? Is the controller only evaluating the GET or POST to call the correct method of the model?

  • 写回答

3条回答 默认 最新

  • drs7798 2011-12-03 19:16

    Short Answer

    Yes. A controller will be responsible for preparing data to display for rendering and sometimes handle GET and POST requests that originating from your client. It should leave HTML generation to the view.

    Long Answer

    MVC can be very helpful in keeping applications maintainable and your code base sane. The pattern helps ensure separation of concerns of your code and in most cases will steer yor clear of 'spaghetti php' where your application logic is tangled with the HTML generation. A sample MVC setup below (there are sure to be many variations of this, but it gives you the idea):


    Responsible for fetching data from the database and saving changes when requested. One example might be a php object that represents a user with name and email fields.


    Responsible for massaging and manipulating data and preparing it for display. The prepared data is passed to a view for rendering, with the view only needing to be aware of just the data it needs to render. For example: a controller may look at a query string to determine what item to fetch to render and combine this with several additional model queries to get additional data.

    Often controllers will also be responsible for handling GET and POST requests that originate from your HTML client and applying some sort of manipulation back on your database. For example - handling form submits or AJAX requests for additional data.


    The view is responsible for actually generating the HTML for display. In PHP, a view would often be a template file with minimal logic. For example, it might know how to loop over items provided to it from the controller or have some basic conditionals.

    Application MVC vs PHP/Python/etc. MVC

    From your other comments it sounds like you are familiar with using MVC in the context of a desktop or mobile application.

    One of the main differences between MVC in the two is the granularity at which the controller is manipulating the view and responding to events.

    For example, in a traditional application the controller might listen for click events originating from a button and respond appropriately.

    When your doing server side html generation however, your controller is only 'alive' for a brief moment while its preparing html to ship out over the wire. This means that it can only do so much to setup and prepare the view for display.

    Instead of listening traditional events from the UI, it can instead react in different ways to future GET and POST requests. For example, you may have a "save" button on a form trigger a POST request to your server (such as yourpage.php?action=save&value=blah). While handling this request your controller might access your models and apply changes, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿