Am I implementing MVC?
Moderator: General Moderators
- Sekka
- Forum Commoner
- Posts: 91
- Joined: Mon Feb 18, 2008 10:25 am
- Location: Huddersfield, West Yorkshire, UK
Am I implementing MVC?
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Am I implementing MVC?
What you describe sounds like MVC yes
Do we get to be nosey and see your code?
- Sekka
- Forum Commoner
- Posts: 91
- Joined: Mon Feb 18, 2008 10:25 am
- Location: Huddersfield, West Yorkshire, UK
Re: Am I implementing MVC?
Erm
Which bit? I don't want to post it all.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Am I implementing MVC?
All of it of course! 
I sounds like you are on the right track. MVC can mean more or less depending on whether you talking about the pattern specifically or about the current practice of MVC or MVC-like implementations. The pattern is mostly about the two separations it defines and the recommended dependencies. The current state of PHP MVC builds a lot on Java Model 2 stuff going back to Struts and before. But then Rails advanced the MVC idea with a number of new ideas like convention over configuration, clean URLs + Action Controllers, ActiveRecords, etc. So when you say "MVC" the answer can be various sizes.
The one thing I notice missing from your implementation is a Front Controller + Action Controllers. It looks like you are using Page Controllers. I also think that if you consider Smarty your View that some actual View code may leak into your Controllers. Not the end of the world, but something you might be aware of.
I sounds like you are on the right track. MVC can mean more or less depending on whether you talking about the pattern specifically or about the current practice of MVC or MVC-like implementations. The pattern is mostly about the two separations it defines and the recommended dependencies. The current state of PHP MVC builds a lot on Java Model 2 stuff going back to Struts and before. But then Rails advanced the MVC idea with a number of new ideas like convention over configuration, clean URLs + Action Controllers, ActiveRecords, etc. So when you say "MVC" the answer can be various sizes.
The one thing I notice missing from your implementation is a Front Controller + Action Controllers. It looks like you are using Page Controllers. I also think that if you consider Smarty your View that some actual View code may leak into your Controllers. Not the end of the world, but something you might be aware of.
(#10850)
- Sekka
- Forum Commoner
- Posts: 91
- Joined: Mon Feb 18, 2008 10:25 am
- Location: Huddersfield, West Yorkshire, UK
Re: Am I implementing MVC?
Don't know what those two controllers are, but I will look them up. Thank you for your input. As I said, I have done very light reading so far on MVC and stopped as I thought I was already hitting the nail on the head.
Anyway... I have included two classes for you to nosey at, my blog post page class and blog post class. I don't want to give away too much of my code because I am a bit paranoid (see next paragraph), so you will be missing a lot of dependencies and other classes.
I am 100% self taught through playing, research and forums. I've never had other developers look through my code, so if it is really s**t, please be honest as I do need to learn.
If for some reason you are bored/interesting and you want to critique my whole system, PM me and I will discuss sending you the files.
Anyway... I have included two classes for you to nosey at, my blog post page class and blog post class. I don't want to give away too much of my code because I am a bit paranoid (see next paragraph), so you will be missing a lot of dependencies and other classes.
I am 100% self taught through playing, research and forums. I've never had other developers look through my code, so if it is really s**t, please be honest as I do need to learn.
If for some reason you are bored/interesting and you want to critique my whole system, PM me and I will discuss sending you the files.
- Attachments
-
- blog.post.zip
- ZIP contains:
class.smweb.blog.post.php
class.smweb.page.blog.post.php - (4.34 KiB) Downloaded 102 times
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Am I implementing MVC?
Just reading your code 
You seem to have your stuff together really well. The class for a blog post looks nicely ORM and easy to read. You seem to have a combined finder/mapper/domain object in the one class which is fine; but it may be worth taking a look at the ActiveRecord pattern which separates these out (I'm not advocating you change your code since it looks good to me). Something a little closer to your design would be Propel. Propel has "Peer" classes which are merely finders, and domain objects which represent the data and perform saves, deletes updates etc.
The "page" class is certainly a controller. The separation between controller and model seems to work beautifully for you. Your View is effectively a smarty template.
Again, it's not really needed in PHP, but people quite commonly wrap $_GET, $_POST and all the other request superglobals in a Request object which the controller uses. I wouldn't advocate changing your code to do that if what you have is working well for you neither. One advantage of using one is that during unit testing you can mock the request.
Your code is actually a pleasure to look at
Psst..
You seem to have your stuff together really well. The class for a blog post looks nicely ORM and easy to read. You seem to have a combined finder/mapper/domain object in the one class which is fine; but it may be worth taking a look at the ActiveRecord pattern which separates these out (I'm not advocating you change your code since it looks good to me). Something a little closer to your design would be Propel. Propel has "Peer" classes which are merely finders, and domain objects which represent the data and perform saves, deletes updates etc.
The "page" class is certainly a controller. The separation between controller and model seems to work beautifully for you. Your View is effectively a smarty template.
Again, it's not really needed in PHP, but people quite commonly wrap $_GET, $_POST and all the other request superglobals in a Request object which the controller uses. I wouldn't advocate changing your code to do that if what you have is working well for you neither. One advantage of using one is that during unit testing you can mock the request.
Your code is actually a pleasure to look at
Psst..
Code: Select all
$class = __CLASS__;
$instance = new $class();
//same as
$instance = new self();- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Am I implementing MVC?
I agree with Chris ... nice and easy to read code. Only two things came to mind. Your Model there seems a little long and it is not clear if it could be multiple classes, but it is probably ok. You combine your Controller and View, so you might want to think about splitting your build*() View methods from your process*() Controller methods.
(#10850)
- Sekka
- Forum Commoner
- Posts: 91
- Joined: Mon Feb 18, 2008 10:25 am
- Location: Huddersfield, West Yorkshire, UK
Re: Am I implementing MVC?
Not 100% sure what you mean. Care to elaborate?arborint wrote:Your Model there seems a little long and it is not clear if it could be multiple classes
Ahh! I understand that bit now. I will look into seperating it. With the page generation and processing data from the page being so closely linked I thought it would be ok to have them in the same class, but I can now see how seperation could help there.arborint wrote:You combine your Controller and View, so you might want to think about splitting your build*() View methods from your process*() Controller methods.
Thanks!
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Am I implementing MVC?
Long classes are a code smell ... but sometimes they just need to be long. The case where you are encapsulating a number of SQL queries is one of them. But you may want to think if splitting it up would make sense if groups of methods are used in distinctly different places in the app.Sekka wrote:Not 100% sure what you mean. Care to elaborate?
(#10850)
- Sekka
- Forum Commoner
- Posts: 91
- Joined: Mon Feb 18, 2008 10:25 am
- Location: Huddersfield, West Yorkshire, UK
Re: Am I implementing MVC?
Hrm...
I will think about that.
Thanks for the advice!
I will think about that.
Thanks for the advice!