示例#1
0
 /**
  * Generate the URL given a route and its parameters
  * @param  Packfire\Router\RouteInterface $route The route the generate the URL from
  * @param  array $params The parameters of the route
  * @return string Returns the generated URL.
  */
 public function generate(RouteInterface $route, $params)
 {
     $rules = $route->rules();
     if (isset($rules['path'])) {
         $path = $rules['path'];
         if (isset($path['uri'])) {
             $uri = $path['uri'];
             $tokens = PathMatcher::createTokens($uri);
             if ($tokens) {
                 $replacements = array();
                 foreach ($tokens as $token) {
                     $name = $token[3];
                     if (isset($params[$name])) {
                         $replacements[$token[0]] = $token[2] . $params[$name];
                     } elseif ($token[4] == '?') {
                         $replacements[$token[0]] = '';
                     } else {
                         throw new MissingRequiredParameterException($route->name(), $name);
                     }
                 }
                 $uri = str_replace(array_keys($replacements), $replacements, $uri);
             }
             return $uri;
         }
     }
 }
示例#2
0
 public function match(RouteInterface $route)
 {
     $result = true;
     $rules = $route->rules();
     if (isset($rules['method'])) {
         $methods = (array) $rules['method'];
         foreach ($methods as $method) {
             $result = strtolower($method) == strtolower($this->request->method());
             if ($result) {
                 break;
             }
         }
     }
     return $result;
 }
示例#3
0
 public function match(RouteInterface $route)
 {
     $result = true;
     $rules = $route->rules();
     if (isset($rules['host'])) {
         $hosts = (array) $rules['host'];
         foreach ($hosts as $host) {
             $result = (bool) preg_match(self::regexCompiler($host), $this->request->host());
             if ($result) {
                 break;
             }
         }
     }
     return $result;
 }
示例#4
0
 public function match(RouteInterface $route)
 {
     $result = true;
     $rules = $route->rules();
     if (isset($rules['path'])) {
         $path = $rules['path'];
         if (isset($path['uri'])) {
             $uri = $path['uri'];
             $paramRules = isset($path['params']) ? $path['params'] : array();
             $tokens = self::createTokens($uri);
             $regex = self::regexCompiler($uri, $tokens);
             $uriParams = array();
             $result = (bool) preg_match($regex, $this->request->path(), $uriParams);
             if ($result) {
                 try {
                     $route->setParams(self::matchParams($paramRules, $uriParams));
                 } catch (InvalidParameterException $ex) {
                     $result = false;
                 }
             }
         }
     }
     return $result;
 }