PHP MVC Frameworks

We all know what a wonderful language PHP happens to be. We also know how unorganized it is. Unless you follow software engineering norms from the very beginnning and isolate all your backend functions from your middle and front end files, very soon you will have nightmarish code where a simple change demands hours of [...]

We all know what a wonderful language PHP happens to be. We also know how unorganized it is. Unless you follow software engineering norms from the very beginnning and isolate all your backend functions from your middle and front end files, very soon you will have nightmarish code where a simple change demands hours of dedicated search and replacement. You have to organize your code into different parts(Models, Views and Controllers). Let’s first explain MVC model

MVC Model

Speaking in classic programming terms, controller is the equivalent of middle end, view the front end, model the backend.

1. The most common use of models (dbtable model) is to declare tables in the database and functions to transact with them, such as INSERT, FETCH records, Delete them. You can also declare functions that act on a single table here(although it is recommended to declare another model for them). For example, you can declare a function that registers a new user on the system by adding his username, password(hashed), email, etc onto a system. You can also declare a function to delete a user from the system.

It is highly recommended that you declare multi table functions i.e queries that access multiple tables in a different model. Also it might be advisable to declare all the models relating to a single context(such as user management, post management) in a seperate model(from the dbtable).

2. Controller: A controller is something that takes care of the interaction between the view and model. It has the most important role in the MVC model. Controller is an object that will process user input, fetch data from models and pass it to the View(the view’s primary function is to display the content i.e rendering the html).
Action:  An action is a part of a controller, its like a divison of a controller. For example a user controller may have an add action(user/add) which displays a form(through the view) to add a new user to the system.

3. View: A view is a file related to an action. It takes care of the formatting of the page, arrangement of content/data. Basically mostly the html. Data is passed on the view by the controller and only data explicity passed by the controller is visible to the view.

Here’s a practical example of how the seperation works: A form is rendered in the view, the input($_ POST,etc) is processed and validated in the controller) , the controller then calls the model to do something with the data. The result from the model is received by the controller and it is parsed and passed on to the view.

Here are a few renowned MVC Frameworks you can try: Please read a guide to them before starting

  1. Cake PHP(recommended for beginners)
  2. Code Igniter
  3. Zend Framework(more control)
There are other models available than MVC too. Such as the event driven programming model. We’ll cover them later