John
John

Reputation: 1085

How does the view communicate with the model?

I am wondering how the view communicates with the model.

As I understand the controller directs the correct information to the model.

class Controller
{
    public function action()
    {
        if(isset($_POST) )
        {
            $this->model->someMethod($_POST['foo'],$_POST['bar']);
        }
    }
}

The model does it's business.

class Model
{
    public function someMethod($foo,$bar)
    {
        // do something
    }
}

The view has to somehow know how to communicate with the model to get it's current state. But how it this done?

class View
{
    public function action()
    {
        // ask the model what is going on
    }
}

How does the view know what happened and if everything went the correct way. I though of getting some state of the model with a 'getState()' method on the model. The state is some string the view knows what to do with But it doesn't seem the right way to me. How does the view know if somebody is logged in for example? Should the view actually know about this?

Upvotes: 1

Views: 611

Answers (2)

tereško
tereško

Reputation: 58444

If you want to explore MVC and MVC-inspired patterns, you should start by reading GUI Architecture by Martin Fowler. Considere it your mandatory literature.


In classical MVC pattern the view observes the model layer for changes in its state. This approach is not really viable in PHP. You still can attempt to apply it, but the benefits will be dwarfed by additional complexity.

There are other approaches.

Model2 MVC (or colloquially known as "Web MVC") gets rid of the observers. Since in web application you have simple request-response workflow and since controllers and views naturally form pairs, you can take an advantage of it. When you know which controller will be used for altering models state, you also at the same time have information about which view will be utilized for the response.

This means that, instead of relaying on the observers, view already knows which parts of model layer could be changed and request the information.

A different approach is called MVP. To explore this, you will be better off if you read the defining publication of the pattern.

Upvotes: 1

newtover
newtover

Reputation: 32094

It is interesting, that it you look at the brief description of the MVC pattern in the GoF's book, or look at the original article about the pattern in SmallTalk, you will see that controller controls the user interaction with the view.

View is usually directly subscribed to the model changes. You can have several views to the same model and each reflects the model changes without any controller.

A controller is something that is attached to a specific view to handle user unput and to transform it into a higher level abstraction (i.e. intention to update something in the model, or pass a message to a subview, or something else).

In a GUI, a view is usually subscribed to the model using some implementation of Observer pattern. In PHP it does not make much sense, since everything is rendered by request (there is usually no shared state between requests), thus, a view can just query a method on the model.

Upvotes: 2

Related Questions