Step 4 – A basic router

Instead of using ‘http://mvctest.localhost/controllers/HelloWorld.php?action=scream’, we should like to be able to browse to ‘http://mvctest.localhost/HelloWorld/scream’. Because we’ve made sure that the controller is created and the action is invoked when using the first url, we only need to rewrite the second to match the first. There is no need for PHP to get involved. An .htaccess file in document root will do the trick.
[snip text-source]code/mvctest/step3/.htaccess[/snip]
The first rule routes ‘http://mvctest.localhost/’ to our HelloWord controller. The second rule will rewrites ‘http://mvctest.localhost/$1/$2′ to ‘http://mvctest.localhost/controllers/$1.php?action=$2′. The [QSA] directive will make sure that other GET variables will also be passed to the script.

Notice that url’s which start with ‘static’ or ‘controllers’ aren’t rewritten. Rewriting ‘controllers’ would cause an infinite loop. The directory ‘static’ is special; it holds all the images, css, js, etc.

I’ve used an .htaccess file for this example, but you may of course also put these rewrite rules directly into you vhost file.

  5 Responses to “Without a framework: (M)VC”

  1. oops… I saw in the post “Perl like temporary variables in PHP”, you said personally you dislike MVC at all?
    confused

    ReplyReply
  2. 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 :)

    ReplyReply
  3. 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

    ReplyReply

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

 

Questions? Just ask!

About the author

Hi, I'm Arnold Daniels. How nice that you like to know a bit more about little old me :).

I've spend a big part of my life behind a computer (and not playing games). I've learned a lot about databases, programming and system administration especially on. the LAMP stack (Linux, Apache, MySQL & PHP).

Have a look at what I'm working on now!
© 2012 Jasny · web development Suffusion theme by Sayontan Sinha