Understanding the MVC Design Pattern in Web Development

Understanding the MVC Design Pattern in Web Development

Written by Daniel John Dunevant on 2020-02-12 23:23:00

What's Going to be Covered

I’ve made a lot of claims about how good MVC is for development. I’d be a fool to not back those claims up. In order to do so I’ll create a hypothetical project and show how it would be laid out in MVC. At that point the proof should be in the pudding. If not I’ve failed as a technology communicator. With my methods and expectations set let’s begin learning about MVC.

Our Assumptions

So let’s make our assumptions. We’re making a news website. There’s users that have accounts. This gives them the ability to comment on the articles posted. There’s of course a blog page as well. There is a homepage that talks about founders of the news agency along with the journalistic integrity that is upheld by it’s writers. Additionally there’s a contact page.

Going Off Our Assumptions

Given these assumptions the directory will have the following structure. A main directory at the root, then 2 other directories account and blog. These will contain the files related to them. Apart from these there will be a model and view folder that are pretty standard for all MVC style software design. Let’s talk about these directories.

The model folder will contain all the calls for data and any logic needed. This type of work is done no where else. So you should be able to see how this makes things a bit more organized. Specifically calls to your database would be done here. In the case of the News website we’re making a lot of blog calls as well as user information calls. This would populate the blog sections and the comments therein.

The view folder is basically for your HTML templates. These templates will be used multiple times and populated with different information based on the commands of the controller. Certain views will be used on most all pages. Such as the header and footer. One last example would be the blog view page. You don’t want to create a new page for each article that is written do you? Me neither. Template that crap and use the URL to carry the blog ID so the DB (Model) knows what blog information to get on your template page.

About the Controller

With that out of the way it’s time to talk about the controller a bit. It is meant to keep things flowing from one page to the next given the information from the model. However the controller does contain a bit of logic. In my implementations I use a switch statement to ‘keep things flowing’. Here’s a example for the root directory controller in code:

  

There's quite a bit to analyse here. Let’s start from the top. First we have the action being read from the URL with: filter_input(INPUT_GET, 'action'). If this is empty the action is set to ‘home’. The first element in the action array is then read in the switch statement and then directs itself to the ‘home’ case. Here it makes one call to the model for the best performing article. This information is then used in the home.php file that is included.

The Bigger Picture

Let’s pretend that the index of account and blog exist and that they use a similar default switch system to set the action. A link that was simply: Blog , or account would take us to the blog and login page respectively. The blog would call for some of the recent blogs. While the login page wouldn’t call anything until the user submits their username and password. Validation should of course be used here but that’s a topic for another blog.

Ending Notes

I think by now you should have a grasp of what MVC is and why it’s so useful as a software style. If I haven’t explained this well enough for you feel free to reach out and I’ll be happy to expound further. Perhaps in another article. For now though see ya next time!



Daniel John Dunevant Dot IO Logo