Clean URLs
OpenAvanti’s preferred method of MVC utilizes clean URLs which map to specific controllers and specific actions, or methods, of that controller.
If you think of a traditional, old school PHP application, your URL would probably look something like this:
http://app.mysite.com/users.php?action=view&user_id=445
This URL is not “clean.” It contains non alphanumeric characters and doesn’t really mean anything to the visitor. While you can still use OpenAvanti and the MVC architecture with these URLs, something like this is a bit prettier, and plainly easy to understand:
http://app.mysite.com/users/view/445
There are plenty of articles out there on the benefits of using clean URLs, but let’s just visit the one that helps us application developers: it makes it painfully easy to cleanly map a URL to a controller and method, as well as pass additional parameters to that method.
Let’s think of the URL in these terms:
http://app.mysite.com/controller/method/argument_1/argument_x
In OpenAvanti, our user/view/445 example gets translated to:
<?php $oUser = new User(); $oUser->view( 445 ); ?>
More on this later. First, let’s discuss how to setup our clean URLs.
Setting Up URL Rewriting
This section covers URL rewriting in Apache using mod_rewrite. Unfortunately, at this time, Apache is the only web browser we provide this information on.
In order to have these clean URLs work, we have to use mod_rewrite. mod_rewrite allows us to take a URL and change it into something else. There are many uses for mod_rewrite, but what we’re going to use it for is to simply take our URI, and pass it as a GET variable to a PHP file that will parse that URL and do something with it.
How we do this is pretty simple, using a few mod_rewrite directives. You can place these directly in the VirtualHost entry in the Apache conf file, or, if it is enabled, you can place them in a .htaccess file:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^/(.+)$ /index.php?request=$1 [QSA,L]Our first directive simply enables the rewrite engine.
We then have a set of rewrite conditions that are matched against our URI. If both of our conditions match, we enter the rewrite directive.
Both conditions combine the document root of our VirtualHost and the URI. For example, if our document root is /var/www/app/pages, and our URI request is /users/view/445, then our combined requested file is /var/www/app/pages/users/view/445.
The first condition determines if the combined string is NOT a valid file on the file system. The second condition determines if the combined string is NOT a valid directory on the file system.
If the combined string is NOT a directory and NOT a file, then we trigger the rewrite rule. The rewrite simply changes our rule to index.php, and appends the query string of request=$1, where $1 is our URI.
Following our users example above, our URL would get rewritten as follows:
http://app.mysite.com/index.php?request=users/view/445
Now in our index.php file, we have access to a GET variable called request which contains the original URI for us to parse. Example:
<?php $sRequest = isset( $_GET[ "request" ] ) ? $_GET[ "request" ] : ""; echo "Request: {$sRequest}<br />"; ?>
Now we can parse that data, instantiate the supplied controller, invoke a method of that controller, and pass along any extra data in the URI (such as the user_id in our example). In OpenAvanti, we use the Dispatcher class to parse, or route, our URIs.

