title: Passing data from MVC Controller to View in PHP
body: <p>I have my own hand-rolled PHP MVC framework for some projects that I'm working on. When I first created the framework, it was in the context of building an admin CMS. Therefore, there was a very nice one-to-one relationship between model, view, and controller. You have a single row in the DB, which maps to a single model. The controller loads the model and passes it to the view to be rendered (such as into an edit form). Nice, clean, and easy.</p>
  
  <p>However, now that I'm working on the front end of the site, things are getting sticky. A page isn't always a view of a single model. It might be a user directory listing with 20 users (each a User model). Furthermore, there might be metadata about the request, such as pagination (current page, total pages, number of results) and/or a search query.</p>
  
  <p>My question is, what is the cleanest way to pass all this data to the view?</p>
  
  <p>Some options I'm considering:</p>
  
  <ul>
  <li><p><strong>Have the controller create an array and pass that to the view as a single parameter:</strong></p>
  
  <pre><code>class UserController{
  
      public function renderView(){
  
          // assume there's some logic to create models, get pagination, etc.
          $data = array()
          $data['models'] = $models; 
          $data['currentPage'] = $current;
          $data['totalPages'] = $total;
          return $view-&gt;render($data);
      }
  }
  
  class UserView{
      public function render($data){
          // render the data
      }
  }
  </code></pre></li>
  <li><p><strong>Create properties in the view class and have the controller populate them:</strong></p>
  
  <pre><code>class UserView{
      public $models;
      public $currentPage;
      public $totalPages;
  }
  
  class UserController{
  
      public function renderView(){
  
          // assume there's some logic to create models, get pagination, etc.
          $view = new UserView();
          $view-&gt;models = $models; 
          $view-&gt;currentPage = $current;
          $view-&gt;totalPages = $total;
          return $view-&gt;render();
      }
  }
  </code></pre></li>
  <li><p><strong>Give the view some sort of generic HashMap or Collection object as a container which can hold any arbitrary number and name of data.</strong></p>
  
  <pre><code>class UserView{
      public $collection = new Collection(); // works like a Java collection
  }
  
  class UserController{
  
      public function renderView(){
  
          // assume there's some logic to create models, get pagination, etc. 
          $view = new UserView();
          $view-&gt;collection-&gt;add($models,'models');
          $view-&gt;collection-&gt;add($currentPage,'currentPage');        
          return $view-&gt;render();
      }
  }
  </code></pre></li>
  </ul>
  
  <p>I know that technically any of the <em>could</em> work, but I'm unsure of the best choice, or if there's a better or more conventional choice that I'm missing.</p>