View Single Post
Old 04-20-2009, 07:35 PM   #69 (permalink)
Enfernikus
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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($routeself::$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;
        } 
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
Tanax (06-10-2009)