An Introduction to MVC
As a programmer, you can take on one or more architectural patterns and one that you may have heard of is the Model View Controller (MVC) pattern. If you are planning on building a functional web application that is easy to test and expand, then you might want to consider using this pattern. I have worked with several PHP frameworks and virtually all of them promote the use of MVC, but you can apply the following information to anything that is based on this architecture.
To help you understand what each component is for, we will be pretending that we are going to build a simple library application where we will be cataloging books.
We are not talking about the people who strut their stuff up and down the catwalk here; In the case of code, a model usually represents a known data structure. For example, our website will need to have books and if you think about all the things a book has, then it will give you a good idea of what a model might look like. Let’s have a look at three simplified models that will relate to each other.
- Front Cover
- Published Date
- Number of pages
- Date of birth
In a real-world environment, you might want to gather more data. Our example models have relationships to each other and each model should have a function that will get the relationship. It is possible that a book could be written by one or more authors, so our book model should have a getAuthors function that will always return one or more authors. Conversely, an author model will have a getBooks function to return all of the books that have been written by that author. Moving onto the publisher the book will be published by one publisher so our book will have a getPublisher method. The publisher will have a getBooks method to get all of the books that they published.
Views are all about what you can see. In theory, when creating a view you should be aiming to just display data and have some minimal logic about what to display. In the world of building apps for the web, the view is usually where you would keep all of the HTML that you want to output. Most of the frameworks that I have encountered encourage you to use templates. A template is a reusable bit of HTML that can be implemented in multiple places across various parts of the application. A view receives all of its data from controllers and then activates other controllers to take you between one view and another.
Finally, we get the controller, nope not the thing attached to your PlayStation, this is a bit of code that controls the flow of information between the model and the view. Generally, you try and make a controller use various other bits of code that do the majority of the work. In a typical web application, it is the controllers’ job to take the request from the browser, decide what to do with it and then create any data that will be needed by the view.
Let’s use our library application example and decide that we want to look at the book called “So Long and Thanks for all the Fish” by Douglas Adams. Now we might have two possible ways to get to this book, via the author e.g. http://mylibrary.com/author/douglas-adams or direct via the book name e.g. http://mylibrary.com/book/so-long-and-thanks-for-all-the-fish. If we went in via the author it would be the controller that then used a database service to retrieve the record for the author Douglas Adams. It would also get a list of all the books that Douglas has written and provided all this information to the view so that you could click on the book that you wanted.
Even though controllers generally do a fair amount of work you want to aim to build small bits of code (usually called services) that you can test independently that do a specific job. I hope that gives you a brief overview as to what MVC is and why you might want to use it. It is my intention to write a little tutorial taking you through how to build a very simple MVC app and in that I will aim to demonstrate why you siphon off bits of code to make these independent services. I might even see if I can touch a little bit on Test Driven Development or TDD for short to show you what benefits it has.
What did you think of my overview? Did any of it make sense or are you sitting in front of your computer with your brain leaking out of your ears because it’s all just gibberish? Would you like to see us do a piece on Test Driven Development and a walkthrough on building a simple MVC application? Give us your feedback using the comments section or via Facebook or Twitter.
Love and rockets