lundi 17 octobre 2016

How to enforce the contract between a controller and a view in PHP

I'm building a PHP webapp and I'm following the MVC pattern as much as I can. So I have a controller file and a view file for each page or element in the page. The model is the database itself.

For example, on my index.php page I do all the business logic beforehand and then I output the view which is expecting a specific set of variables:

index.php

<?php
// all the business logic here
// setting up the variables that the view is expecting

// output the view
require("index.view.php");
?>

index.view.php

    <div>
       <h1>
          <?php echo title; ?>
       </h1>
    </div>

So now my question is, I can easily figure out the exact number and the names of all the variables that the view file is expecting (in this case just one, the title). However, for very large and complex view this can became more and more difficult, very tedious and prone to name clashing with other views.

How can easily enforce this interface between the controller and the view and make it clear (from the controller) which variables the view needs? Using objects instead of a plain set of varibles? Using a function which echoes the view and take as parameters the needed variables? Or what?

Aucun commentaire:

Enregistrer un commentaire