tags:

views:

601

answers:

8

I would like to implement MVC from scratch in PHP because I want full control of my own code and no extra bagage from existing frameworks. Anyone who has any advice?


Yes, I've seen Lerdorfs article and it seems that it ain't so much code after all. Actually I would more like to have a controller-view solution for structuring my application. I'll stick to my own homemade PDO data-access classes.

+1  A: 

This could be a good starting point. "Understanding MVC in PHP" is a tutorial covering the basics of MVC. A great tutorial!

code-zoop
A: 

if you're just going to "recyle" the wheel, you can take a look at the source code of "popular" frameworks. if you want to "reinvent" the wheel, i suggest you look elsewhere. examine domain-specific languages (DSL).

stillstanding
+8  A: 

Your question somewhat smells like Not-Invented-Here-Syndrome. In this case, my advice would be to live with the extra baggage of existing frameworks when you can be sure they are thoroughly tested and supported. Don't reinvent the wheel.

On the other hand, the above argumentation would prevent new frameworks to be written. And writing one from scratch is a good coding exercise to learn and understand the MVC pattern.

So if you are really determined to do it, my suggestion is to learn what each part of MVC is, does and how they interact. You will inevitably come across the FrontController pattern as well, so you will want to learn about this one too.

Note that you are not the only person wanting to do this:

And there is also this interesting article by Rasmus Lerdorf

Gordon
All mvc frameworks looks a bit overkill for many kind of small-to-medium applications, so yes, 'reinvent the wheel' could be a good choice.
DaNieL
@DaNieL Define *all* please. Wikipedia alone lists 50+ PHP MVC frameworks with a number of them explicitly stating to be lightweight, fatfree, small footprint, minimalist, etc.
Gordon
@Gordon While I agree with your points in general, most frameworks for PHP are currently uniquely awful, with some of the worst code I've seen in any published source and I've yet to see one I would touch that is not hideous (except perhaps Zend) so I think the question is reasonable in the case of PHP. Hopefully this will change. Suggestions welcome.
Iain Collins
@Iain then stop looking at Cake and CI (and check out http://www.php-frameworks.net/) ;) Also, what makes you think the OP won't create horrible code either?
Gordon
#gordon: sorry, my bad: with 'all' i mean the 'usually-suggested' most famous frameworks, ci, cakephp, etc..
DaNieL
Don't reinvent a existing application either? That's because I'm a programmer!!! I would like to do it myself, home-cooked tastes better!!!
poo
@Gordon I was thinking of those two in particular as it happens (though not the only offenders). ;) php-frameworks.net seems interesting, will take a look...
Iain Collins
@poo Well, programmers seek to solve problems in an efficient way. Reinventing the wheel isn't efficient. Don't get me wrong though: I'm not saying *don't do it*. I'm saying don't do it *for the wrong reason*. *Home-cooked feels better* carries the bias and misconception that self written code is superior code (*all the others devs are idiots*), which is rarely true. I think it's cool to write your own MVC for learning or just for the fun of it. If you can spare the time, go ahead. You'll learn a lot. But unless the outcome excels at something, leave it as that: an exercise.
Gordon
+1  A: 

I personally use my own framework consisting of :
1.Mysql Interface
2.Template System (yes home brewed not smarty)
3.Config Class (mysql details,debug, and anything else that the script might need)
4.Simple Form Creating class.
5.a Request Class (all useful details from $_SERVER in a more readable format ex: $this->Request->ip, $this->Request->url,$this->Request->time)
6. Anti-hacking (Ip blacklist, keywords from public sec. scanners etc.)
And I just call it framework :)

DCC
great job, that's the way to go...
poo
A: 

You can also check this PHP MVC Tutorial.

php html
A: 

I, too, wrote an homegrown MVC framework in PHP. Its pretty simple, especially when you remove any "ActiveRecord" functionality from your frame work. Some things that I considered:

How are you going to map URLs to controllers?

Instead of doing things by convention (/foo maps to FooController), I did everything via configuration. That is, I have a master routes.php file wherein I list every possible URL that my application will accept. So its filled with things like:

Router::add( '/foo/:Param1/:Param2', 
             array( 'Controller' => 'MyController', 
                    'Action' => 'my_method', 
                    'Method' => 'GET', 
                    'Parameters' => array( 'Param1' => '\d+',
                                           'Param2' => '\S+' ) );

In this case we match urls like /foo/123/abc. When the URL is matched, it is dispatched as MyController::my_method( array( 'Param1' => '123', 'Param2' => 'abc' ) );.

How are you going to generate views?

There are lots of templating systems out there. But really, PHP is already a perfect templating system. In my framework, I just created a function template() in the top-level Controller class. And it all boils down to performing an include $Template. Again, in my framework, there is no convention. Each controller is responsible for instantiating the appropriate template, and for understanding if the request is expecting HTML, XML, or JSON as a response.

Can you use an existing framework?

A lot of my code was inspired by Cake, the well-known PHP MVC framework. I'd definitely take a peek at it before you proceed to far. If you're going to roll your own, at least start by understanding how all of the popular ones work. In the end, the peculiar requirements of my application made me go down the road of build my own, but there was a lot to be learned from all the frameworks already out there. Take a long look around, and you may find something that works for you. At the very least, you may figure out exactly what you need out of your framework.

Peter Kovacs
A: 

There is a project called 'Simple PHP Framework'

It is as you say a very basic implementation of MVC. It is based on Rasmus article. And it may be great starting point.

Sinan
+1  A: 

A simple exemple implementation of MVC (just to understand the principle)

MODELE: lib/Thing.class.php

class Thing
{
//class code ( CRUD, the application logic ...)
}

VIEW: theme/page_thing.php

<?php require("header.php");?>
//HTML CODE with some echo to show variables and boucles to read arrays
<?php require("footer.php");?>

CONTROLLER: application/thing.php

require_once("lib/Thing.class.php");
/*
Somme controls between the Model and the View ( if/else ...)
*/
include("theme/page_thing.php");
Amirouche Douda