tags:

views:

305

answers:

5

Hi, I want to create my very first web app using HTML, MySQL, PHP and the Model View Controller. It will be very simple for starters: a couple of pages that allows users to input/delete data to/from the database and display the contents of the database.

However I am new to MVC, and I really wish I could have found MVC tutorials appropriate for beginners like myself on how to actually implement it using simple code examples. I only found advanced examples which makes the learning curve unnecessarily steeper. I guess I will have to create one here.

My questions are, how do I get started with designing this MVC from scratch for a very very simple web app? How do I separate the Model, the View and the Controller in code? What steps should I take and how? How do I implement the model as business entity classes, the view as HTML, and the controller as scripts that glue everything together? (assuming my understanding of this is correct) Can anyone recommend clear and straight forward tutorials on how to actually implement MVC for beginners like myself? (I could not find any).

Thanks in advance.

+2  A: 

There are plenty of frameworks to help you with this. I would recommend using CakePHP, http://www.cakephp.org. It has good documentation, a large community and a lot of tutorials!

dale
Please note that most of current PHP "MVC" frameworks implements something that's not a true MVC. In other words: they implement that pattern wrong. At least Symfony/ZF/Kohana doesn't handle it. I'm pretty sure CakePHP (I've never used it) doesn't implement it correct as well.
Crozin
Thanks for your reply Dale. I saw CakePHP as one of many frameworks available for PHP. I will start researching that now. Just curious though, is designing MVC from scratch that difficult? Thanks again for recommending CakePHP
01010011
@Crozin, thanks for your reply. How did you learn MVC: from scratch or a framework? Which will benefit the beginner more? Which framework would you recommend if any? Thanks for your insight
01010011
@dale, CakePHP looks interesting
01010011
@dale, CakePHP's documentation looks good so far
01010011
@01010011: I've learnt from the scratch.You asked me what PHP FW I could recommend. Well... I'm using [Symfony](http://symfony-project.org) - it's a really good and powerful FW with great community.I think it's a better idea to learn from scratch cause it's important to know how some things work. When you're learning form FW you learn only how to use these "tools", not how they work. Such basic knowledge is required for every programmer.
Crozin
@Crozin, I will look at symfony, thanks. Can you recommend any tutorials for learning from scratch for the beginner?
01010011
+1  A: 

There is no quick path for this.

If you really want to build an MVC, go over some existing ones. Read their documentation and try understanding why they made certain choices (security, robustness, etc).

You will find here a good list of existing MVCs.

Am
@Am, I'm reading the CakePHP now and it looks interesting.
01010011
+2  A: 

A good tutorial to understand how MVC works is this one. It shows you how to create a framework from scratch but I'd suggest you use an existing framework if you want to do something more complex.

Ed
@eduardoks, thanks for your reply and the link. I am diving head first into CakePHP and when I am ready, then I will spend time trying to create MVC on my own.
01010011
+1  A: 

When you want to do MVC the first time, you do not start from scratch. CakePHP is a really good recommendation by Dale to start with. When you have done it many times and understand what MVC is about, then you will be able to implement an MVC without a framework.

sibidiba
Thanks for your reply @sibidiba. Judging from your repsonce it sounds like MVC is complicated, especially for beginners. I am new to PHP. When I think about this MVC, I thinking in terms of C++ classes and how to modularize my code to seperate the 3 parts. Yeah, I like the documentation so far, logged onto the IRC chat already, and I cannot wait to see what CakePHP is like.
01010011
MVC is not complicated at all. I guess you already know the concept of it. But it is different to use, and to implement an MVC framework. For the latter you have to deeply understand MVC's concept, and the language you are working with, and the typical usage scenarios.Never ever start from scratch, except if you really, really have to.Thinking that your code without any prior experience will be better and cleaner and more functional then the well tested code of dozens of experienced programmers is a mistake every programmer (including me) makes way too often.
sibidiba
For CakePHP, this is a great resource: http://book.cakephp.org/And this: http://api13.cakephp.org/classesWhat I really like in CakePHP, that is is so simple, that you can easily look into its source code when you are in doubt how it works.
sibidiba
+1  A: 

This could be a good starting point: MVC in PHP is a tutorial covering the basics of MVC.

php html