Step 5 – An advanced router
The chosen router is extremely simply, but you might want a more complex routing. Usually you should be able to find a combination of RewriteCond’s and RewriteRule’s to solve you problem. But in some case you might need a script to supply the routing rules. Does this mean there’s no escape of the single entry file method, no it doesn’t (unless you are on a public server, because that this might not work).
Apache allows you to use an external program to act as a rewrite map. I prefer to use PERL for these kind of things, but this can also be done with php.
[snip php-source]code/mvctest/step5/myrouter[/snip]
The router is not a script which is run on each request, but an application running alongside Apache. Therefore it is not possible to define this rewrite map in the .htaccess file, instead you need to add the following to your vhost file (sorry):
RewriteEngine On RewriteMap myrouter prg:/var/www/mvctest/myrouter
In the .htaccess file we now only need to use the rewrite map with a rewrite rule and all is done.
[snip php-source]code/mvctest/step5/.htaccess[/snip]
—-
* Design patterns are described in ‘Patterns of Enterprise Application Architecture’ by Martin Fowler (ISBN 0-321-12742-0)


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