Mod_rewrite

Filed in Lab Leave a comment

I wanted to start a small MVC framework to help build my next project. I needed to get mod_rewrite working and also build a simple class loader.

For mod_rewrite I setup the following .htaccess file (xp.htaccess to get around Windows’ filename issue)

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(php|css|js|gif|png|jpe?g)$
RewriteRule (.*)$ /index.php [L]

Then in my dispatcher file I can read the contents of the query string like this:

$request_uri = explode('/', $_SERVER['REQUEST_URI']);
$controller = htmlentities($request_uri[1]);
$action = htmlentities($request_uri[2]);
if($action == '') {$action = 'index'; }
$param = htmlentities($request_uri[3]);
if($param == '') {$param = ''; }

This is just temporary code and used to prove that it would work the way I expected. Sort of like tracer bullets described in The Pragmatic Programmer. But now I can use a URL like:

http://recipemanager/ingredient_type/update/5

Which will then let me know that I need to access the ingredient_type controller pass the parameter 5 to the update method.

TOP