Without a framework: (M)VC
Thanks to the rise of the PHP frameworks the Model View Controller pattern is becomming quite populair. To get a good idea how this works and what job PHP and Apache have, I’ve written a short example Hello World application which implements the MVC model without the use of a framework.
—-
I’m working on a PHP framework for a dutch IT company; Bean IT. This framework does not support the Model View Controller* pattern yet. I’ve got a DB abstraction layer which implements the model in the form of Active Record’s*, but there are no Controllers or Views. I like the MVC pattern, as well as the clear url’s which come with the routing solutions in most frameworks.
The solution I see most, is that Apache always rewrites a request to the same PHP file (single file entry point), after which a router object in the PHP script takes over and calls the action on the correct controller. This method is recommended in tutorials of codeignitor and zend framework (and probably also other frameworks).
The 2 mayor drawbacks of the ’single file entry point’ method are, that static loading is nearly impossible and lots of classes are loaded even for the simplest page request. Both of which are bad for performance. Read these 2 blog posts to understand why: “Remember: be nice to byte code caches” and “How fast is your framework”.
Let’s try to find a solution that does allow static loading.
12 Dec 2006 Arnold Daniels




oops… I saw in the post “Perl like temporary variables in PHP”, you said personally you dislike MVC at all?
confused
Hi Jefflee,
I think it is not really possible to dislike something you haven’t tried. I’ve tried MVC for a few projects and did not like it. Especially for websites it is doing more bad than good. Still my remark at the other post might be a bit to harsh. MVC might be a good strategy for other types of web-apps.
Arnold
PS. You’re reading my blog really closely. Kudos to you
Hi Arnold,
First of all, thank you very much for your post, altough I found very late. I think as you are, about ’single file entry point’ and I searched about other ways to reach VC components only.
I like your way. But I have some comments. Your .htaccess file is not working on my apache server. Therefore I used my .htacess as given below:
RewriteEngine On
RewriteRule ^$ /controllers/helloworld.php [QSA] [L]
RewriteRule ^([^/\.]+)?$ controllers/$1.php [QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)?$ controllers/$1.php?action=$2 [QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ controllers/$1.php?action=$2&var=$3 [QSA]
If you are any advice about your .htaccess modification, I will be appreciated.
And the another point for views. How can we use multiple template with your approach?
Regards