Am I implementing MVC?
Posted: Sat Mar 01, 2008 8:00 pm
I've been reading up on MVC to see what it actually is. And from what I have read, I think I am already implementing it in one form or another. What I want to know is am I right?
Let me try and explain how I have built my system. I'm going to use my personal website and the blog within it as an example. Apologies for the length.
Model?
Firstly, I have classes for each 'object' associated with my blog, e.g. a blog post, a comment, a tag, a user, etc. These classes load data from the DB by being passed certain variables, e.g. an ID, and store the data accordingly. I also have admin classes for each object that extend the originals, and these classes provide added functionalty such as editing the data and storing it back in the DB.
I also have 'gateway' classes. These load groups of the above objects, e.g. most recent blog posts. The gateway classes have queries themselves, and pass the result arrays of data into the above classes from which objects are created. (That make sense?)
View?
Basically, I'm using Smarty. Smarty is passed read only objects from my 'controller' and generates the HTML accordingly.
Controller?
I have classes that are linked to each type of page in my website. Let me use a page of my blog as an example. The page class loads an instantiation of a blog post class using data from the URL. Other data is loaded in the same manner. All the relevant objects are then passed into Smarty and the page is loaded.
On this blog post page, there is a facility to add comments. This same page class handles the form submission. It takes the data from the POST and passes it to a new admin blog post object (normal blog post objects are read only). All the validation is done within the admin object to ensure encapsulation. The page then tells the admin object to add itself to the database.
So...
This is a very quick example, but do you think I am implementing MVC or am I even near to implementing it already?
Thank you and apologies again for the length.
Let me try and explain how I have built my system. I'm going to use my personal website and the blog within it as an example. Apologies for the length.
Model?
Firstly, I have classes for each 'object' associated with my blog, e.g. a blog post, a comment, a tag, a user, etc. These classes load data from the DB by being passed certain variables, e.g. an ID, and store the data accordingly. I also have admin classes for each object that extend the originals, and these classes provide added functionalty such as editing the data and storing it back in the DB.
I also have 'gateway' classes. These load groups of the above objects, e.g. most recent blog posts. The gateway classes have queries themselves, and pass the result arrays of data into the above classes from which objects are created. (That make sense?)
View?
Basically, I'm using Smarty. Smarty is passed read only objects from my 'controller' and generates the HTML accordingly.
Controller?
I have classes that are linked to each type of page in my website. Let me use a page of my blog as an example. The page class loads an instantiation of a blog post class using data from the URL. Other data is loaded in the same manner. All the relevant objects are then passed into Smarty and the page is loaded.
On this blog post page, there is a facility to add comments. This same page class handles the form submission. It takes the data from the POST and passes it to a new admin blog post object (normal blog post objects are read only). All the validation is done within the admin object to ensure encapsulation. The page then tells the admin object to add itself to the database.
So...
This is a very quick example, but do you think I am implementing MVC or am I even near to implementing it already?
Thank you and apologies again for the length.