What is MVC

Joomla makes the use of design patterns to enforce consistency, reusability, and familiarity across the code base. Most of the code that you want to write for the components will fit nicely into the model View Controller also known as MVC design pattern.

Model View Controller is a design pattern. It's not a specific piece of code as much as it is a way of organizing code into familiar reusable pieces. Design patterns are designed to do just that just to take your code and break it down into reusable parts that are interchangeable so that no one part is really depending on the other too much.

The concept of design patterns that computer science has is taken directly from the world of Build architecture. There's a book called A Pattern Language that came out in the 70s and there's a quote from it that really sums up what design patterns are and why you use them.

Each pattern describes a problem that occurs over and over again in our environment and then describes the core of the solution to that problem in such a way that you can use the solution a million times over without ever doing it the same way twice.

And you are going to find in computer science that this is extremely true no two frameworks out there implement the model View Controller design pattern in the same way and you're going to find design patterns out there in code that are quite like the ones that you saw the last piece of software that you wrote code for.

So how does model View Controller work?

Model View Controller splits your application into three primary tasks. The one task for the controller is to receive the user input all it to doing is receiving the user input and then telling the view and the model what to do. So after you've receive the user input then you have your model which is processing any data this is typically where you're going to talk to your database if you have one or where you're going to pull data in for a third-party source and the only thing the model is doing is processing that data and getting it ready for either the database or the view and then finally the view is primarily concerned with displaying output. You pretty much want to keep just view code in your view and you don't want to do any data processing in there because that really is the model's job. Unfortunately a lot of PHP scripts that you'll find out there do the opposite of this. You'll have a piece of code up at the top that's connecting to the database and trying to validate the data and do all of these things in the event that a form has been submitted and then at the bottom of that file you'll find a bunch of HTML markup that's a form that's the one that people are submitting to and it's all in one big script. And it just becomes a huge mess because it's impossible to maintain and you have designers that try to come in and change the markup and you run the risk that they are going to break the data processing that's happening up at the top of the file.

So for your Web applications you really want to use something like model View Controller, because it breaks it out into the separate parts that don't have to rely on each other directly.

Let's take a look at some pseudo code PHP examples of how you might write model View Controller code.

  1. <?php
  3. class Controller{
  4. public function save(){
  5. $data = $_REQUEST['data'];
  6. $model = $this->getModel();
  7. $model->save($data);
  8. }
  10. public function delete(){
  11. $id = $_REQUEST['id'];
  12. $model = $this->getModel();
  13. $model->delete($id);
  14. }
  15. }


So first there is the controller and all its doing is taking in user input and it gives the user a selection of different things that can be done. So functions for this controller you can either save or delete records and all the controller is doing is getting the data from the user and passing it right along to the model. That's the most that this controller is doing and you really want to have same controllers, so that all of the controller is doing, is focusing on receiving the user input.

  1. <?php
  2. // MODELS
  3. class Model{
  4. public function save($data){
  5. $validated = validate_data($data);
  6. $this->table->save($validated);
  7. }
  8. public function delete($id){
  9. $key = validate_id($id);
  10. $this->table->delete($key);
  11. }
  12. public function load($id){
  13. $key = validate_id($id);
  14. $this->table->load($key);
  15. }
  16. }


So next we have the model and the model is going to receive the data and then connect with the database and either save it or delete it or modify it. Anytime we want to connect to the database you really want to use a model because you don't want your controller to be responsible for working with the data in the database

  1. // VIEWS
  2. <div class="record">
  3. <h3><?php echo $data->title; ?></h3>
  4. <div class="content">
  5. <?php echo $data->content; ?>
  6. </div>
  7. </div>


And then finally there is a view. the view is primarily concerned with the HTML output that goes back to the browser and you don't want to connect to the database at all here you just want to focus on the HTML markup.

MVC Concept

So another way of thinking of the model View Controller design pattern is an entertainment system in your home. Consider your remote control, your television, and your DVD player. Your remote control is solely interested in receiving input from what channel or what DVD you want to watch it's not storing the actual movie or the television program and it is not displaying anything it's just receiving your input and talking to the DVD player and the television. So then you have the DVD player itself and the DVD player is just concerned with the reading data from the DVD and sending it to the television, it also receives some commands from the remote control that you have and it will either turn on or stop or play or start depending on what the controllers is telling it to do. And then finally you have your television which acts as the view, the television does not contain the television program or the movie that you're watching it's simply displays what the DVD player, the model is feeding it.

The MVC style libraries included in Joomla make it easy to take your component from concepts to working code.