View Full Version : writing an effective router/dispatcher for mvc pattern ??

01-24-2008, 10:51 AM
hello forums !!
I am trying to make an router/dispatcher for oo mvc pattern.
I would like to have one default router pattern ie controller/method/id and other is customized one
for example

http://yoursite.com/controller/method/id/params => $controller->method($id[, $params])(general)
http://yoursite.com/admin/controller/method/id/params => $controller->admin_method($id[, $params]) (customized)

Can anybody provide good suggestions/samples in dispatching such method.
Note: dispatching method should work both for url rewriting turned on or off by changing some flags.

Thanks in advance for your valueable suggestions.

01-25-2008, 12:19 PM
Knock Knock... Can anybody come with better ideas

01-25-2008, 03:53 PM
I was creating a blogging system and created something like this. You will have to customise it but here it is:


echo '<p>This is our index.php file!</p>';

// Requested URL

// Removes App Root from URL
$url = str_replace('/BlogSite/', '', $url);

$routes = array(

array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit')


$params = array();

$route_match = false;

foreach($routes as $urls => $route) {

if(preg_match($route['url'], $url, $matches)) {

$params = array_merge($params, $matches);

$route_match = true;



if(!$route_match) { exit('No Route Found!'); }



I used a .htaccess file so all my /dirs/ get passed through the index.

Hope this helps.

01-25-2008, 10:05 PM
Any Good?