Simple Routing Library
Once I've wrote a simple routing library. You know, the one that you can use to make pretty urls such as: http://ferdianto.com/2009/05/21/this-is-a-post. Actually, framework like CodeIgniter has internal support for friendly URLs. But, what if you already have an up-and-running application and decide to make pretty urls. This library is intentionally made simple so that it can be included in any framework or application you have.
The library is inspired by Django — web framework build in python language. So, you only have to specify the URL pattern (perl-compatible-regex) and handler. Here's some simple usage:
include 'route.php';
//a simple controller class
class HelloController
{
function hello()
{
echo 'Hello World';
}
}
//a simple controller class
class TestController
{
function test()
{
echo 'test';
}
}
//a simple controller function
function index()
{
echo 'Hello World';
}
//a simple controller which accept parameters
function testparam()
{
echo '',print_r( func_get_args() ),'
';
}
$urls = array(
//simple function
'!^$!' => 'index',
//using string as classname
'!^test/$!' => array( 'TestController', 'test' ),
//using object
'!^hello/$!' => array( new HelloController, 'hello' ),
//use parameterized regex
'!^archives/(\d+)/(\d+)/(\d+)/(.+)$!' => 'testparam',
//simple include
'!^about$!' => 'about.php',
);
route($urls);
Requirement for this library to work is mod_rewrite. And the configuration is pretty simple, I've just use the wordpress .htaccess configuration. Here's the rewrite syntax:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php [L]
Interested? You can download the source via link below.
http://beta.ferdianto.com/media/route.php
If we have an up and running web already, then we can restructure and redesign the web to have friendly URLs.
That is new project, I think.
And new income.
sweet! so thats how it works :p
lets make it as simple as possible bro