I thought it was about time that I wrote another post and after a lot of the projects I have been working on lately I thought why not write about some MVC/Frameworks related stuff. I have plans to write some how-to and provide sample sites including code, built in my current favourite, the Zend Framwork.
This post however is more about help novice, new or programmers who just can’t get whats so great about the whole MVC (Model / View / Controller) style of coding.
The problem I faced when learning about MVC was that I had no idea what it really was, I had read plenty of articles and tutorials but I was still left with questions that were best answered in a conversation I later had with a fellow developer.
If someone had told me from the start in basic terms what a Model, View and Controller were, how they interacted, what kind of code would each have and why would I want to use them then I would of been trying to use them from day one, but instead, for months I would hear about it and look at articles but not really grasp the true beauty of MVC.
To really get start with MVC its best to use something that implements it and has documentation. What I am talking about is a framework, when using a framework, building something like a Model, View or Controller really isn’t that hard because they have documentation but the main reason is that MVC is purely a concept of programming and not really any specific implementation. This is similar to the concept of using “pseudo code” to learn programming, its not to teach you any specific language, but instead to teach you the ideas and concepts behind programming and why you do things a certain way.
What is a Model? - the best way to think of a model is that it is a table in the database which is used a similar way to an object. This means that you could instantiate a model and could call methods like find() and save() to find records and save records. This I wold have to say really is a core concept of the Model, that it represents a table as an object in your code. Its really that simple.
How would I use a Model? - Although it is normally framework specific, in the Zend Framework you would create a class that implements the Framework’s ‘Model Class’ and that would provide you with access to the Framework’s Methods that you can use to do things like find data, save data, update data, remove data and anything else that is related to the database.
What is a Controller? - A Controller is basically your central processing unit, it takes in things from the view, like data submitted in a form put in a View file and will create a Model object and start to put the data into the database through the Model object. If you were going to display data in a View then you would first in your Controller, instantiate a Model object, then you would use a method of that object to find the required data, then you would pass it into your View by setting it to as variable that your View can access.
What is a View? - A View is basically like a template file, its mainly HTML, has basic PHP code to print the data that you have passed into it and will probably have some foreach loops to display rows data.
As you can start to see, there are some real benefits in terms of breaking code up into small, modular chunks and having separate parts handle separate tasks.
I will throw together a simply setup of a site with some form processing and database functionality and explain what I have done and provide you with all the source code so you can modify it into what ever you want to help you learn or build a site using an MVC Framework
Enjoy.

mark