Example #1
0
 /**
  * Parses given URL and returns an array of controllers, action and parameters
  * taken from that URL.
  *
  * @param string $url URL to be parsed
  * @return array
  */
 function parse($url)
 {
     // An URL should start with a '/', mod_rewrite doesn't respect that, but no-mod_rewrite version does.
     // Here's the fix.
     if ($url && '/' != $url[0]) {
         $url = '/' . $url;
     }
     $out = array();
     $r = null;
     $default_route = array('/:controller/:action/* (default)', '/^(?:\\/(?:([a-zA-Z0-9_\\-\\.]+)(?:\\/([a-zA-Z0-9_\\-\\.]+)(?:[\\/\\?](.*))?)?))[\\/]*$/', array('controller', 'action'), array());
     $this->routes[] = $default_route;
     foreach ($this->routes as $route) {
         list($route, $regexp, $names, $defaults) = $route;
         if (preg_match($regexp, $url, $r)) {
             // $this->log($url.' matched '.$regexp, 'note');
             // remove the first element, which is the url
             array_shift($r);
             $out['pass'] = array();
             // hack, pre-fill the default route names
             foreach ($names as $name) {
                 $out[$name] = null;
             }
             $ii = 0;
             if (is_array($defaults)) {
                 foreach ($defaults as $name => $value) {
                     if (preg_match('#[a-zA-Z_\\-]#i', $name)) {
                         $out[$name] = $value;
                     } else {
                         $out['pass'][] = $value;
                     }
                 }
             }
             foreach ($r as $found) {
                 // if $found is a named url element (i.e. ':action')
                 // Modified by Dan S.
                 if (isset($names[$ii])) {
                     if (in_array($names[$ii], $this->keywords)) {
                         $out[$names[$ii]] = $found;
                     } else {
                         $out['extra'][$names[$ii]] = $found;
                     }
                 } else {
                     $out['pass'] = array_merge($out['pass'], Z_Array::array_remove_empty(explode('/', $found)));
                 }
                 $ii++;
             }
             break;
         }
     }
     return $out;
 }