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?

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):

Model

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.

Controller

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.

View

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.

Do I have understand something completely wrong?

Yes.

The MVC pattern is not for the browser. The browser sees HTML anyways. Whether this HTML is generated with C++, PHP, Java or whatever it doesn't matter. The browser doesn't care what design patterns were used to generate this HTML.

MVC is a design pattern to organize and separate responsibilities in your code. So it's not for the browser, it's for the developers writing the code. It's to create more maintainable code where you have a clear separation between your business logic (Model), the communication between the model and the view (Controller) and the UI (View).

Whether you should use the MVC pattern in your web site is a subjective question that I prefer not to answer as it will depend on many factors.

I realise that I am answering a very old questions, however, I do not believe that any other question has answered your initial concern appropriately, and this will hopefully add something useful for others who may stumble across this question in their learning about MVC.

Firstly, I will start by saying I read an interesting article about the confusion which exists within the PHP community about MVC and it is worth a read if you are asking this type of question.

Model-View-Confusion part 1: The View gets its own data from the Model

Secondly, I do not believe you have misunderstood anything at all, rather you have a better understanding of PHP and MVC which is allowing you to ask the question. Just because something has been defined and accepted by the community at large, it does not mean you should't question its use from time to time. And here is why.

There is NO memory between requests (except for SESSION state) within a PHP application. Every time a request is received the entire application fires up, processes the request and then shuts down i.e. there are no background application threads left running in the background (at least none which you can use within your application) where you could, theoretically, maintain application state. This is neither good, nor bad, it is just the way it is.

Understanding this, you can probably start to see what you are thinking is correct. Why would a View (which is allowed to access its own data from the Model) need a Controller? The simple answer, is that it doesn't. So for the pure display of a Model, the View is perfectly entitled to fetch its own data (via the Model) and it is actually the correct way to implement MVC.

But wait. Does this mean we don't need Controllers? Absolutely NOT! What we need to do is use a Controller for its appropriate purpose. In MVC, the Controller is responsible for interpreting user requests and asking the Model to change itself to meet the users request, following this, the Model can notify it's dependencies of the change and the View can update itself. Obviously, as you know, in the context of a PHP web application, the View is not just sitting and waiting for update notifications. So how can you achieve the notification?

This is where I believe MVC got hijacked. To notify the View it's Model has changed, we can simply direct them to the URL of the View which accesses the now updated Model. Great, but now we have introduced something into the Controller. Something which says, "Hey, I'm a controller but now I have knowledge of the location of the View." I think at this point, someone thought, "why do I bother redirecting the user? I have all the data which the View needs why don't I just send the data directly to the View?" and bang! Here we are.

So let's recap.

  1. A View CAN access the Model directly within MVC
  2. The Model houses the business logic for the Domain Objects
  3. A Controller is not meant to provide the View access to the Model or act as any type of mediator
  4. The Controller accepts user requests and makes changes to it's Model
  5. A View is not the UI/HTML, that is where a Template is used

A practical example To explain what I have been describing, we shall look at a simple, commonly found function within most websites today. Viewing the information of a single logged in user. We will leave many things out of our code here in order to demonstrate just the structure of the MVC approach.

Firstly lets assume we have a system where when we make a GET request for /user/51 it is mapped to our UserView with the appropriate dependencies being injected.

Lets define our classes.

// UserModel.php
class UserModel {
    private $db;

    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function findById($id) {
        return $this->db->findById("user", $id);
    }
}

// UserView.php
class UserView {
    private $model;
    private $template;
    private $userId;

    public function __construct(UserModel $model, Template $template) {
        $this->model = $model;
        $this->template = $template;
    }

    public function setUserId($userId) {
        $this->userId = $userId;
    }

    public function render() {
        $this->template->provide("user", $this->model->findById($this->userId));
        return $this->template->render();
    }
}

And that's it! You do not require the Controller at all. If however you need to make changes to the Model, you would do so via a Controller. This is MVC.

Disclaimer

I do not advocate that this approach is correct and any approach taken by any developer at any point in time is wrong. I strongly believe that the architecture of any system should reflect its needs and not any one particular style or approach where necessary. All I am trying to explain is that within MVC, a View is actually allowed to directly access it's own data, and the purpose of a Controller is not to mediate between View and Model, rather it is intended to accept user requests which require manipulation of a particular Model and to request the Model to perform such operations.