Friday, August 7, 2015

MVC for PHP Developers

What is MVC?

MVC, or Model-View-Controller is a software architecture, or design pattern, that is used in software engineering, whose fundamental principle is based on the idea that the logic of an application should be separated from its presentation. Put simply, I would say that MVC is simply a better way of separating the logic of your application from the display.

The pattern’s title is a collation of its three core parts: Model, View, and Controller. A visual representation of a complete and correct MVC pattern looks like the following diagram:
MVC Process

The MVC principle is to separate the application into 3 main parts, known as the Model, the View, and the Controller. Apparent from the diagram are the direct associations (solid lines) and the inferred associations (dashed lines). The inferred associations are associations that might seem apparent from the point of view of the user, and not from the actual software design.
A simple way to think of this would be to consider the following:
  • A user interacts with the view - by clicking on a link or submitting a form.
  • The Controller handles the user input, and transfers the information to the model
  • The Model receives the information and updates it's state (adds data to a database, for example, or calculates todays date)
  • The View checks the state of the Model and responds accordingly (listing the newly entered data, maybe)
  • The View waits for another interaction from the user.
But what does this mean to you and why should you consider using it?
Well, for starters, MVC has a really good philosophy. The idea that you are separating the logic from the display is not new, but MVC presents the idea nicely. Code presentation and layout are simpler, making your application more maintainable. The view is in the view files, the logic in the template, and the controller handles them all.

Business Logic

This term amuses me, because it implies something is happening that doesn't really have a definition that can be defined properly. However, it is a simple concept: Business Logic is the process of calculating the logical processes of an application. A simple calendar's business logic would be to calculate what todays date is, what day it is, and on what day all of the days in this month fall, for example.
Don't let yourself get bullied by flashy terms. Business logic is the processing part of the application.

Templates

Many MVC frameworks use some sort of template system to enforce the principle of DRY (See Below), making it really easy to re0use code without having to rewrite it.
I have seen MVC frameworks that run on Smarty, or their own template engine, or none at all. A simple warning is that some template engines have rather complicated syntax - check them out before you start developing, you don't ant to learn a whole new language just to get a page to render.

DRY

Another very good implementation of MVC is the DRY (Don't Repeat Yourself) philosophy. Essentially, DRY is used by Ruby on Rails and a few other implementations, and the idea is that you write something once and once only, re-using the code. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."
Correct implementation of DRY would imply that changing one element of the system does not change unrelated elements, which is rather logical. Personally, I think Ruby on Rails pulls this off in the best way, and does it most simply.

Convention Over Configuration

Convention over Configuration is a design paradigm that essentially tries to remove the amount of decisions you, as a developer, need to make. This is achieved by setting up the framework with the conventions that all elements usually require. The developer only needs to change the things that really need to change.
It's quite simple, if you think about it. Consider a form: it has elements that are always required, and those elements have states that are usually the same. A form has a <form> tag, which defines an action, method, name, id and enctype, for example. Unless you need to change something, it is pretty easy to get the form name, id and action from the url (usually). We can also set all form methods to POST unless otherwise stated. Applying this idea to all elements makes building this type of application very fast, easy and simple.

No comments:

Post a Comment