Ik ben bezig met een eigen MVC. In mijn view heb ik een aantal variabele die gevuld moeten worden. Dit gebeurt naar mijn inziens in de controller, maar de manier waarop ik het nu doe vind ik nog niet heel netjes.
Iemand tips? :)


// news.php
<html>
<head>
<title><?php echo $title; ?></title>
</head>

<body>
<h1><?php echo $title; ?></h1>
<?php
foreach($newsitems AS $newsitem)
{
  echo "<h2>".$newsitem["title"]."</h2>";
  echo "<p>".$newsitem["description"]."</p>";
}
?>
</body>
</html>

// newsitem.controller.php
<?php
class NewsitemController
{
  private $_model;

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

  public function getAll()
  {
    $title = "Nieuws";
    $newsitems = $this->_model->getAll();

    include "views/news.php";
  }
}


?>
Je moet een View layer in je classes inbouwen. Deze laad de template, zorgt voor de variabelen, etc.

Ong. zoiets:
<?php
class ViewModel
{
public $parameters = array();
public $template;

public function render($template = null, array $parameters = array())
{
extract(array_merge($this->parameters, $parameters));

include $template ?: $this->template;
}
}

// in controller
public function allAction()
{
$newsItems = $this->_model->getAll();

return $this->viewModel->render('views/news.php', array(
'title' => 'Nieuws',
'news_items' => $newsItems,
));
}
?>

Reageren