Dynamic routing is actually not as complex as one might think if one only uses straight regex and not wild cards.
Let us say we use a URL object with stores all data after index.php so for example
index.php/store/category/10293/animals/fluffybird
The url object would store store, category, 10293, animals, fluffybird within an array or an object.
Let us say we have this
index.php/category-1029-animals-fluffybird.html
and regex like this
/cagtegory-(.*[0-9)-(.*[a-zA-Z)-(.*[a-zA-Z0-9])\.html/
=>
/store/category/$1/$1/$1
Before we call the controller method, we call the routing object which simply scrolls though an array of regular expressions and if they match my routing object produces an array like this
controller =>store
method =>category
than the arguments are 10293, animals, fluffybird in an arguments array. My routing object than goes a step further and injects these into the url object replacing the single array item
with
category-10293-animals-fluffybird
with an array that looks like this
category, 10293, animals, fluffybird. After this, the returned controller/method array is used in place to call the controller method
This is part of the function that I use to route
PHP Code:
while( (list($route, $val) = each(self::$route)) && $continue == true )
{
if( in_array($route, self::$isRegex) )
{
if( preg_match($route, $urlString) )
{
$urlString = preg_replace($route, $val, $urlString);
$continue = false;
}
}else{
$regex = '/' . $route . '/';
if( preg_match($regex, $urlString) )
{
$urlString = preg_replace($regex, $val, $urlString);
$continue = false;
}
}
}
//now break the url string again and return the controller, the function, and the variables
$return = array();
$urlString = explode('/', $urlString);
$url = count($urlString)-1;
//Build an array with our controller, method, and varaibles for input
$return['controller'] = $urlString[0];
$return['method'] = $urlString[1];
$urlObject = Factory::getLibrary('url');
$urlObject->feed(0, $urlString[0]);
$urlObject->feed(1, $urlString[1]);
for($i = 2; $i <= $url; ++$i)
{
$return['vars'][] = $urlString[$i];
$urlObject->feed($i, $urlString[$i]);
}
Factory::feedObject($urlObject);
return $return;
}