101-series

Development 101: What is MVC Pt. 2

by Jamal on February 25, 2013

So in the first part of this article we really didn’t cover that much in terms of details. Now don’t you feel like your back in class? O how the memories of sitting in class and my professor just talking out of their ass mostly because the only reason they’re teaching is for the research money the university gives them, or because it just seems nice to teach on the side. Seriously there are professors like this, I happen to have a few :). In part 2 of the What is MVC intro section to Development 101 we are going to take a nice deep dive into all the nitty gritty details. If this was a class, this would be like picking into the brain of the classmate who was smart enough to find his information online rather than the professor or a dated textbook.

How Models, Views, and Controllers interact

 

MVC Interaction With Browser

In the image above, we get a great visual representation of how Models, Views, and Controllers interact with each other when the server is being called by the visitors browser. This is a more Rails oriented demonstration, but after reviewing this you should have everything you need to know about the basics. Also, I’ll be posting images after this that showcase different PHP frameworks like CodeIgniter.

Breaking it down

  • Browser gets in touch with the web server
  • Server goes through routes in rails to find what code needs to pulled for this URL
  • The appropriate Controller then talks to its designated Model
  • The Model then pulls data from the database, and sends it back to the controller
  • The Controller then takes that data and pulls the appropriate Views to display the data
  • The code from the Views contacts the calling Controller and then the Controller interacts with the server
  • The server then displays what would be a webpage to the user in their browser

Now wasn’t that easy to break down!

Below, you’ll find another image that breaks down the MVC interaction with the Browser.

 

CodeIgniter MVC interaction with browser

Keep your Models FAT, and your Controllers skinny

 

Isn’t weird being in an industry where we believe the fatter the model, the better your life will be? Okay bad joke, but I had to do it.

Let’s take a moment and look back at the image breaking down how the MVC architecture interacts with its parts. You see the Controller, and it looks to be like a gateway for the three different areas: the Model, View, and Server. Noticing that is a good catch, because that is how you should consider your controllers. As traffic directors! Your controllers job should be only to act as the middle man between all the calls the other three areas in the architecture make to each other.

Now your probably wondering what exactly goes into the Model. For Models, their job is to hold all the logic that you’re writing for your application. So in simpler terms, this is where all the serious programming take place.

 

What about the Views?

 

Well, Views are the HTML that is written to display things in the browser of the user. The CSS, JS, and images are hosted in a public folder that is accessed the same way as http://yoursite.com/images.

 

That’s all folks

 

Well between reading part 1 of this piece, and getting everything fine tunned with this latest one, the MVC section of this 101 class is done! Now its time to actually move into writing some application level code. But before class is dismissed, I have to ask one question……

What would an application be without the database?