Example #1
0
 /**
  * Connects a new route to some URL pattern.
  * URLPattern might have "variables", which has a ":" at the beggining. e.x. ":action"
  *
  * E.x.:
  * <code>
  * $router->connect(":controller/:action/:id", array(), array("id" => "[0-9]+"));
  * </code>
  *
  * The route above will map to following URL's:
  * item/add/34
  * post/view/9233
  *
  * But not to these:
  * item/add/AC331
  * post/view
  *
  * URLPattern variables :controller :action :id by default might have value from
  * a range [_.a-zA-Z0-9]. When you pass array("id" => "[0-9]") as varRequirements
  * id is required to be only numeric.
  *
  *
  * @link http://www.symfony-project.com/book/trunk/routing
  *
  * @param string $URLPattern
  * @param array $defaultValueList
  * @param array $varRequirements
  *
  */
 public function mapToRoute($URLStr, Request $request)
 {
     if ('/' == substr($URLStr, -1)) {
         $URLStr = substr($URLStr, 0, -1);
     }
     if (empty($URLStr) || !$this->isURLRewriteEnabled) {
         if (!$request->isValueSet("action")) {
             $request->set("action", $this->defaultAction);
         }
         if (!$request->isValueSet("controller")) {
             $request->set("controller", $this->defaultController);
         }
         return false;
     }
     foreach ($this->routeList as $route) {
         $routePattern = str_replace('\\047', '\\/', $route->getRecognitionPattern());
         $routePattern = str_replace('.', '\\.', $routePattern);
         if (preg_match("/^" . $routePattern . "\$/U", $URLStr, $result)) {
             unset($result[0]);
             $varList = $route->getVariableList();
             foreach ($varList as $key => $value) {
                 $request->set($value, $result[$key + 1]);
             }
             $requestValueAssigments = $route->getRequestValueAssigments();
             $request->sanitizeArray($requestValueAssigments);
             $request->setValueArray($requestValueAssigments);
             return $route;
         }
     }
     throw new RouterException("Unable to map to any route");
 }