views:

1906

answers:

9

I need a simple and basic MVC example to get me started. I dont want to use any of the available packaged frameworks.

I am in need of a simple example of a simple PHP MVC framework that would allow, at most, the basic creation of a simple multi-page site.

I am asking for a simple example because I learn best from simple real world examples. Big popular frameworks (such as code ignighter) are to much for me to even try to understand and any other "simple" example I have found are not well explained or seem a little sketchy in general.

I should add that most examples of simple MVC frameworks I see use mod_rewrite (for URL routing) or some other Apache-only method. I run PHP on IIS.

I need to be able to understand a basic MVC framework, so that I could develop my own that would allow me to easily extend functionality with classes.

I am at the point where I understand basic design patterns and MVC pretty well. I understand them in theory, but when it comes down to actually building a real world, simple, well designed MVC framework in PHP, i'm stuck.

I would really appreciate some help!

Edit: I just want to note that I am looking for a simple example that an experienced programmer could whip up in under an hour. I mean simple as in bare bones simple. I dont want to use any huge frameworks, I am trying to roll my own. I need a decent SIMPLE example to get me going.

+1  A: 

CodeIgniter is generally considered one of the lightest and simplest MVC frameworks.

cam8001
That's way bigger than what I need that's exactly what I am avoiding. Besides, I want to roll my own.
Allen
+5  A: 

You can also try Kohana, which is a derivative of CodeIgniter that is more friendly with the OOP concept of PHP5 (with things like autoloading of classes and such). We used it successfully with many projects where I work.

http://www.kohanaphp.com/

The documentation could give more examples, but overall it's pretty good.

Laurent Bourgault-Roy
Even Kohana is to much, I am looking to make my own, I just need some simple code to get me going in the right direction.
Allen
+2  A: 

CakePHP has reasonably good documentation, and a very handy feature where it will automatically "bake" your project for you - setting up all the view/add/edit/delete pages and actions for you. In a real project, you wouldn't want to use the generated code, but it's really handy so you can see how the basics work: take the baked code and work from there.

nickf
+2  A: 

CakePHP is a good one that emulates Rails. The aforementioned CodeIgniter is a similar framework. If you desire simplicity though, you might consider whether or not you need a framework at all. WordPress actually works quite well as a CMS for simple websites.

Droo
I would call CakePHP anything other than simple.
cletus
+9  A: 

I am at the point where I understand basic design patterns and MVC pretty well. I understand them in theory, but when it comes down to actually building a real world, simple, well designed MVC framework in PHP, i'm stuck.

I think I know why you're stuck. You're stuck because MVC as it was originally conceived about twenty years ago doesn't actually work on the web. Every MVC web framework you've come across is actually a kludge, a hack. They are MVC frameworks because that's the name that people have chosen to use, but it's definitely not MVC.

You don't need an MVC framework for the web, and trying to find one will make you feel like you are trying to fit a square peg into a round hole. What you really need is to look at a very simple request-response handling framework.

The simplest one I know that is worth studying is not written in PHP, but in Python. It's called Albatross. It actually does a lot more than request-response handling, because it includes a template engine as well. But the session handling stuff is well done and that's what's important.

The problem that you will need to overcome on the web is the stateless nature of HTTP. MVC by definition requires state to be passed between the views and the models cleanly and quickly, which is why the two don't mix well. MVC is a compound design pattern that was defined and understood to apply to objects sharing the same memory space. If you can't use the observer pattern to link up your models and views, you aren't doing MVC! It can't be mapped on to the web easily.

If you want to keep up the MVC paradigm on the web, you will need to define for yourself what a view is and what a controller is. Most popular web frameworks have completely bastardised the notion of a view, it's basically a "page" or some nonsense. Traditionally a view would be quite a smaller component of what you see, for example a single text box or a single table. So, in the web world, you're better off thinking of DOM elements as views.

For the controller, best bet is to use JavaScript to implement your controllers. Build your models using the RESTful principles of the HTTP protocol. So your URIs are paths to model objects and the HTTP methods of GET, PUT, DELETE, and POST become operations done on those objects

Is it much harder than the way current web frameworks work? Yeah, because you can't crib code and ideas from popular code. But web frameworks are good for blogs and photo galleries.

If you need to build an enterprise application with conventional web frameworks or even with conventional paradigms and your own framework with hundreds or even thousands of model objects with a large persistence layer and hundreds of templates, permission controls, user roles, reporting, importing and exporting of data, scalability, databases full of hundreds of gigabytes of user data, etc you will quickly go insane.

I know this because that's the kind of app I work on every day, and I am slowly going insane.

RibaldEddie
I have to disagree with MVC "doesn't actually work on the web". It may be different for web applications than desktop, but when used properly (skinny controllers, fat models for example), it is an invaluable technique. Since I've started using MVC a year ago for everything from small to very large apps, I haven't regretted it once. It's been a huge turning point for me as a developer and I cringe to think of programming without it.
Arms
My point is that Model-View-Controller object relationships as they are conventionally known don't apply to the web in a meaningful way.I'm not suggesting that you don't separate your concerns, just that separating them the way MVC describes the separation is impossible, and it's certainly not comparable with the way its done on the desktop. The tools and frameworks for doing it on the web are weak and anemic compared to the desktop.
RibaldEddie
To sum it up, the web "MVC" frameworks are fine, but incorrectly named.
Bart van Heukelom
I more or less stand by my claim that any framework that doesn't place both the View Classes and the controller classes in the client are horrible to work with and definitely not MVC frameworks. The two ones I know of that are MVC frameworks for the web are Cappuccino and extJS. Do you know any others that work the same way? Also, you still need something on the server to manage your REST API and I really like Albatross and SQLAlchemy for that sort of stuff.
RibaldEddie
+2  A: 

Allen, I know you think CodeIgniter isn't simple, but it really is one of the more simple ones out there, and I would recommend you check it out. You can also check out the Recess framework (http://www.recessframework.org/). I haven't looked at it in-depth, but I've watched their video tutorials a few times and it seems like an easy enough framework to learn. However, its community is small, and if you have questions you won't find answers as easily as you would with a framework that has a larger community.

As for your concern about running on IIS, I would imagine (or hope) that most frameworks would take this into consideration. I know the Zend Framework does. It basically boils down to using Isapi Rewrite (on IIS's end) and using $_SERVER['HTTP_X_REWRITE_URL'] (instead of `$_SERVER['REQUEST_URI']) in PHP.

I recommend reading the Surviving the Deep End book (http://survivethedeepend.com/ - it's free!). It's about getting up and running with Zend Framework, but he does a very good job explaining MVC on the web. Check out chapters 2 and 3.

Now I'm just gonna throw in my opinion about writing your own framework. Don't. I did this a year ago, and while it was a terrific learning experience, I've decided to migrate to Zend Framework. The reasons were simple: other people had already written code that I would have needed to write myself. In other words: don't reinvent the wheel. It was great fun writing my own framework, but naturally there were a number of bugs. Using a premade framework will provide you with stable, tested code. And better yet, if it has a strong community, you'll always have a healthy source of information. I feel that by reading up on theory, and asking yourself (and figuring out) how Framework X does "this" or "that", you can learn just as much as if you had written your own. I chose Zend Framework because it's easily extendable, I can write my own code parallel to ZF's, and be confident that I'm building on a solid foundation.

Arms
I understand what you are saying but I still want to build my own. In my case I am trying to find the right balance of complexity. No framework is to little, but any, even the simplest ones, framework that is available as a package is just to much.
Allen
+1  A: 

http://kissmvc.com its one 12k file

or micromvc.com

N Con
A: 

I am actually at the same point as Allen. Indeed, CI is simple, I used it for my last project (really small thing) and I was pretty happy with it.

I don't want to reinvent the wheel but I'd also like to create my own framework for my projects. Yes, I could use CI or any other framework and that would save me a lot of time but I find it hard to fully understand how the framework works by only using it. This is why I want to develop my own, to understand the mechanism.

Here is a link I found really interesting: http://develturk.com/2009/04/24/writing-a-php5-mvc-framework-like-code-igniter-part1-controller-view-registry-loader/

Allen, please keep me updated if you find anything else that could help!

Toby
+1  A: 

Here is a simple MVC tutorial, http://phpro.org/tutorials/Model-View-Controller-MVC.html

nikhil