1

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?

4

2 に答える 2

2

興味深いことに、 GoF の本で MVC パターンの簡単な説明を見るか、またはSmallTalk のパターンに関する元の記事を見ると、コントローラーがビューとのユーザーの対話を制御することがわかります。

ビューは通常、モデルの変更を直接サブスクライブします。同じモデルに対して複数のビューを使用でき、それぞれがコントローラーなしでモデルの変更を反映します。

コントローラーは、特定のビューにアタッチされて、ユーザーの入力を処理し、それをより高いレベルの抽象化に変換するものです (つまり、モデル内の何かを更新したり、サブビューにメッセージを渡したりする意図があります)。

GUI では、ビューは通常、オブザーバーパターンの実装を使用してモデルにサブスクライブされます。PHP では、すべてがリクエストによってレンダリングされるため (通常、リクエスト間で状態が共有されることはありません)、あまり意味がありません。したがって、ビューはモデルのメソッドをクエリするだけです。

于 2013-10-07T12:36:05.663 に答える