This is perhaps shameless self-promotion, but...
I've written a little framework which (amongst other things) attempts to mimic Django's URL dispatch functionality in PHP. It's not quite done yet (documentation is really minimal right now), but it's getting there... You may find the wiki article on URL dispatching, the URL resolver and invocation code (see also here) helpful.
With that said, here is a little recipe to get you started: Let's say you have a configuration file containing mappings between regular expressions and callbacks:
<?php
return array(
'{^blog/(?<year>\d+)/(?<slug>[\w-]+)$}' => array('BlogController', 'post'),
'{^about$}' => array('AboutController', 'about'),
// and so on...
);
Iterate over this array and match the keys against the request URL:
<?php
foreach($urlconf as $pattern => $callback) {
if (preg_match($pattern, $_SERVER['REQUEST_URI'], $matches) !== false) {
return dispatch($callback, $matches); // match
}
}
return handle_404(); // no match
The dispatch()
function should/could instantiate the controller class and and inspect the method using reflection. This would typically involve validating the URL pattern matches with respect to the parameters of the method in question. You would need to collect the arguments with which to invoke the method with: filling in default arguments if present, figuring out which named or positional match correspond to which parameter, etc. (See this code for an example.) Then, the controller method can be invoked using the arguments from the URL pattern match.
Hope this helps!
EDIT: Of course, mod_rewrite
or the $_SERVER['PATH_INFO']
trick is needed to send all requests to the same .php
file. A suitable .htaccess
file can be found here.