Passing data from MVC Controller to View in PHP

14,962

I'm going to recommend the concept of Fat Models, Skinny Controllers (or, Fat Models Thin Controllers if you prefer...)

In otherwords, your model is too strict - tying your model to represent only something like a RowDataGateway is extremely limiting.

In fact, I think good models hide the fact that you're reading the data from a database at all. Because, in reality, your data could be in text files, or from a web service, or whatever. If you treat your Model like nothing more than a glorified DBAL, you doom yourself to having tightly-coupled code in your controllers that just won't let you break away from the "data only comes from the database" way of thinking.

Share:
14,962
Warren Benedetto
Author by

Warren Benedetto

I attended Cornell University, earning a Bachelor's degree in Evolution And Human Behavior. I didn't minor in anything, but if I had, it probably would have been something equally useless. After graduation, I moved to Los Angeles to pursue a career in being famous. Upon my arrival, I was informed that there were already enough famous people, and my services would not be needed. Never one to be deterred, I enrolled in USC and earned a Master's degree in TV/Film Writing. "Now can I be famous?" I asked. "No, sorry," Los Angeles said. "But you can be unemployed, if you'd like." Equipped with $200,000 in debt and zero marketable skills, I taught myself graphic design, PHP, MySQL, HTML5, CSS3, JS, UX, UI, NodeJS, and a bunch of other acronyms. Then I convinced some people that I know what all those letters mean. Luckily for me, most companies looking for a designer or developer want someone who is unusually handsome, athletic, and charming. When they can't find anybody like that, they hire me instead. I have 11+ years of experience developing, designing, and marketing web apps and sites from concept to completion. As Creative Director at Gaikai, I helped to launch their groundbreaking cloud gaming service, leading up to their acquisition by Sony Computer Entertainment for $380 million in July 2012. I'm currently their Director Of User Experience working on a project so secret, even I don't know what I'm doing. I'm also the developer of StayFocusd, the popular Google Chrome productivity app. I haven't had any spare time since 1994, but if I did, I would spend it writing screenplays, playing professional basketball, becoming the next great white rapper, and napping.

Updated on June 17, 2022

Comments

  • Warren Benedetto
    Warren Benedetto almost 2 years

    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.

    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.

    My question is, what is the cleanest way to pass all this data to the view?

    Some options I'm considering:

    • Have the controller create an array and pass that to the view as a single parameter:

      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->render($data);
          }
      }
      
      class UserView{
          public function render($data){
              // render the data
          }
      }
      
    • Create properties in the view class and have the controller populate them:

      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->models = $models; 
              $view->currentPage = $current;
              $view->totalPages = $total;
              return $view->render();
          }
      }
      
    • Give the view some sort of generic HashMap or Collection object as a container which can hold any arbitrary number and name of data.

      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->collection->add($models,'models');
              $view->collection->add($currentPage,'currentPage');        
              return $view->render();
          }
      }
      

    I know that technically any of the could work, but I'm unsure of the best choice, or if there's a better or more conventional choice that I'm missing.