Skip to main content

MVC: Display requstedrequested content via the View

Tweeted twitter.com/StackProgrammer/status/741697456224796672
added 763 characters in body
Source Link
MikeMason
  • 319
  • 2
  • 7

Update I just found this online, which seems to suggest the View should 'pull' the data from the Model?

class View
{
    private $model;

    public function __construct($model) {
        $this->controller = $controller;
        $this->model = $model;
    }

    public function output(){
        $data = "<p>" . $this->model->tstring ."</p>";
        require_once($this->model->template);
    }
}

This is confusing as Paul Hegarty on iTunes U – 'Developing Apps for iOS' (lecture video 1), he says the Model and View should never communicate and the information flow should always travel via the Controller? But the example about shows direct communication between the View and Model.

Update I just found this online, which seems to suggest the View should 'pull' the data from the Model?

class View
{
    private $model;

    public function __construct($model) {
        $this->controller = $controller;
        $this->model = $model;
    }

    public function output(){
        $data = "<p>" . $this->model->tstring ."</p>";
        require_once($this->model->template);
    }
}

This is confusing as Paul Hegarty on iTunes U – 'Developing Apps for iOS' (lecture video 1), he says the Model and View should never communicate and the information flow should always travel via the Controller? But the example about shows direct communication between the View and Model.

Source Link
MikeMason
  • 319
  • 2
  • 7

MVC: Display requsted content via the View

I'm trying to learn how the MVC pattern works so have been playing around a bit. I just wrote this, which was making sense to me, until I wanted to display the array content which has been collect by the Controller class via the View class output() method.

<?php 

class Model {

    public $arrayPeople = array(
        1 => 'Dave',
        2 => 'Burt',
        3 => 'Jack',
        4 => 'Michael',
    );

    public $arrayAnimals = array(
        1 => 'Fish',
        2 => 'Cat',
        3 => 'Dog',
        4 => 'Tiger',
    );

}


class Controller {

    private $model;

    public function __construct( $model ){

        $this->model = $model;

    }
    
    public function getArray( $arrayName )
    {
        return $this->model->$arrayName;
    }

}


class View {

    private $model;
    private $controller;

    public function __construct( $model, $controller ){

        $this->model = $model;
        $this->controller = $controller;

    }

    public function output()
    {
        echo '<a href="?action=getArray&value=arrayAnimals">arrayAnimals</a><br/>';
        echo '<a href="?action=getArray&value=arrayPeople">arrayPeople</a><br/>';
    }


}


$model = new Model;
$controller = new Controller( $model );
$view = new View( $model, $controller );


if (isset($_GET['action']) && !empty($_GET['action'])) {
    $controller->{$_GET['action']}($_GET['value']);
}


echo $view->output();