Step 2 – Calling actions
The script is a bit too simple. The controller should have more actions than ‘index’ and there should be a general function which handles the controller initialisation.
[snip php-source]code/mvctest/step2/controllers/HelloWorld.php[/snip]
The script now includes an file ‘init.inc’ which holds the function to initialise the controller: mvc_start(). This function is called after the class is defined, but only if the script was the entry file.
If there would be a HelloMars controller which would extend HelloWorld, ‘HelloMars.php’ would be the entry file and therefore only that script would call mvc_start(), not ‘HelloWorld.php’.
[snip php-source]code/mvctest/step2/init.inc[/snip]
The mvc_start() function gets the class name based on the entry file and creates the controller object. If the class doesn’t exist, a 404 ‘page not found’ HTTP error is displayed.
The function will look for a GET variable ‘action’, taking “index” as default. It will invoke the action on the controller object.
Browsing to ‘http://mvctest.localhost/controllers/HelloWorld.php’ will still show “Hello World”. Adding ‘?action=scream’ will show “AAAAAAHHHHH”.


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