I want to learn MVC "architecture pattern" but I don't want to jump into a framework like Rails or Django just yet. I want to understand the concept first and write some simple code in my currently familiar environment, which happens to be PHP/HTML/CSS/MySQL. I don't necessarily need a tutorial that is based on PHP, as I do understand a lot of different languages. And I don't want to have to install any frameworks or APIs or libraries. I just want to learn how to think in MVC and apply it to my projects. Any suggestions?
-
1I've just posted a ["barebone MVC PHP application"](http://php-mvc.net/) on GitHub that was made for exactly these learning reasons. – Sliq Dec 21 '13 at 09:42
5 Answers
Almost every framework does MVC differently, so you might end up getting even more confused. The general principles of MVC are very simple: "Model is state; view reacts to model; controller reacts to view; controller changes model". The model, view and controller are concepts - they are whatever you feel them to be. Classes, bunches of classes, instances of classes with XML configuration files, you name it.
I actually think that about covers the basic principles. Without a framework, you'd not get much further. What matters is how a particular framework defines model, view and controller and their interactions.

- 25,685
- 3
- 53
- 85
-
Are you suggesting that I can't take an existing project and rewrite my code in an "MVC" way without using an existing framework like Rails or Django? – Ixion Sep 30 '08 at 08:34
-
3Frameworks are concrete manifestations of the abstract idea of MVC. Even if you don't use a pre-fab framework, you would be creating your own manifestation along the way. – troelskn Sep 30 '08 at 10:36
-
6That's fine. I just don't want the extra baggage of learning someone else's framework. I want the theory, the mindset. That's how we used to do CS back in the day. I don't mind building up a rudimentary custom framework. If that's what it takes, so be it. – Ixion Oct 01 '08 at 00:24
-
1Not sure I understand how this answers the question. Does that mean you think he can't? – dkretz Mar 01 '09 at 05:26
-
I'm pretty sure he doesn't say that he can't, but he's saying that he'll end up writing a "framework" of his own, using the ideas he described. – cwap Jan 06 '10 at 17:46
MVC is basically just splitting up your code into a Model, which deals with the data, a View which displays the data, and a Controller which passes data from the Model to the View.
It's nothing you need an API or framework for, it's just a way of splitting up your code. The reason many frameworks use it is because it's a very simple concept, it works well for many things (it fits webpages perfectly), and is fairly flexible (for example, with Rails, you could do everything in your view, or model/controller, if you so-desired..)
A quick example in python, of an example MVC structured Python script. Not necessarily "best practices", but it works, and is fairly simple:
class Model:
def get_post(self, id):
# Would query database, perhaps
return {"title": "A test", "body": "An example.."}
class Controller:
def __init__(self):
self.model = Model()
self.view = View()
def main(self):
post = self.model.get_post(1)
self.view.display(post)
class View:
def display(self, item):
print "<h1>%(title)s</h1>\n%(body)s" % item
c = Controller()
c.main()

- 165,801
- 69
- 278
- 343
-
I'm not sure how simple it is after all. Picking up a programming language is fairly simple to me, it's all about the syntax. But I am still am having trouble understanding the bottom line of the thing... I'm hoping for an euphony. – David Jan 02 '10 at 04:47
-
In theory MVC seems simple,in fact the problems start with php-syntax and how each framework organizes classes, controllers and views (sometimes different languages are used for views and integrated with js),for an overview I suggest this [article](http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762) – maioman Dec 30 '14 at 00:19
In addition to Sander's reply, I'd say that most frameworks confuse front controller and MVC. They are really two completely separate concepts, but they are often both present in frameworks. So watch out for that.

- 115,121
- 27
- 131
- 155
-
1Agree! +1, Front controller is a mvc-ish pattern mostly used on the web. As far as I know Rails, ASP.Net MVC, ASP.Net monorail Django and the likes are all front-controller. – Mendelt Sep 30 '08 at 12:41
-
The main advantage of MVC is separation of concerns. When you write code, and if you're not careful, it can become a big mess. So knowing how to put Models, Views, and Controllers in different "silos" saves you time in the long term. Any strategy is good.
So here is mine :
- models are files found under /lib in the project tree
- views are files ending in .html in the project tree
- controllers are urls in <form> action attributes
Know it is late but I am sure people will come along later with the same question.
I think the very good code example above is better put like this but YMMV:
#!/usr/bin/python
class Model:
def get_post(self):
return {"title":"A test","body":"An example.."}
class View:
def display(self,items):
print 'Title:',items['title'],'\n'+'Body:',items['body']
class Controller:
def __init__(self):
self.model=Model()
self.view=View()
def main(self):
post=self.model.get_post()
self.view.display(post)
mvc=Controller()
mvc.main()
Here is another example using inheritance which can be very useful in python/php.....
#!/usr/bin/python3
class Control:
def find(self,user):
return self._look(user)
def _look(self,user):
if user in self.users:
return self.users[user]
else:
return 'The data class ({}) has no {}'.format(self.userName(),user)
def userName(self):
return self.__class__.__name__.lower()
class Model(Control):
users=dict(one='Bob',two='Michael',three='Dave')
class View():
def user(self,users):
print(users.find('two'))
def main():
users=Model()
find=View()
print('--> The user two\'s "real name" is:\n')
find.user(users)
if __name__=="__main__":
main()
If this makes sense go to django now your ready. Just read the free book if this makes sense you will go through it rapidly. Your right though you must be able to know about OOP and MVC paradigms before using django as it is built and used via these paradigms.
As you see it is not complex it is just one of the many ways to keep your code in order.

- 81
- 1
- 1