Exemplo n.º 1
0
 /**
  * Match given request _url and request method and see if a route has been defined for it
  * If so, return route's target
  * If called multiple times
  *
  * @param string $requestUrl
  * @param string $requestMethod
  *
  * @return bool|Route
  */
 public function match($requestUrl, $requestMethod = 'GET')
 {
     foreach ($this->routes->all() as $routes) {
         // compare server request method with route's allowed http methods
         if (!in_array($requestMethod, (array) $routes->getMethods())) {
             continue;
         }
         $currentDir = dirname($_SERVER['SCRIPT_NAME']);
         if ($currentDir != '/') {
             $requestUrl = str_replace($currentDir, '', $requestUrl);
         }
         // check if request _url matches route regex. if not, return false.
         if (!preg_match("@^" . $this->basePath . $routes->getRegex() . "*\$@i", $requestUrl, $matches)) {
             continue;
         }
         $params = array();
         if (preg_match_all("/:([\\w-%]+)/", $routes->getUrl(), $argument_keys)) {
             // grab array with matches
             $argument_keys = $argument_keys[1];
             // loop trough parameter names, store matching value in $params array
             foreach ($argument_keys as $key => $name) {
                 if (isset($matches[$key + 1])) {
                     $params[$name] = $matches[$key + 1];
                 }
             }
         }
         $routes->setParameters($params);
         $routes->dispatch();
         return $routes;
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * Match given request _url and request method and see if a route has been defined for it
  * If so, return route's target
  * If called multiple times
  *
  * @param string $requestUrl
  * @param string $requestMethod
  *
  * @return bool|Route
  */
 public function match($requestUrl, $requestMethod = 'GET')
 {
     foreach ($this->routes->all() as $routes) {
         // compare server request method with route's allowed http methods
         if (!in_array($requestMethod, (array) $routes->getMethods())) {
             continue;
         }
         $currentDir = dirname($_SERVER['SCRIPT_NAME']);
         if ($currentDir != '/') {
             $requestUrl = str_replace($currentDir, '', $requestUrl);
         }
         $route = rtrim($routes->getRegex(), '/');
         $pattern = "@^{$this->basePath}{$route}/?\$@i";
         if (!preg_match($pattern, $requestUrl, $matches)) {
             continue;
         }
         $matchedText = array_shift($matches);
         $params = array();
         if (preg_match_all("/:([\\w-%]+)/", $routes->getUrl(), $argument_keys)) {
             // grab array with matches
             $argument_keys = $argument_keys[1];
             // check arguments number
             if (count($argument_keys) != count($matches)) {
                 continue;
             }
             // loop trough parameter names, store matching value in $params array
             foreach ($argument_keys as $key => $name) {
                 if (isset($matches[$key])) {
                     $params[$name] = $matches[$key];
                 }
             }
         }
         $routes->setParameters($params);
         $routes->dispatch();
         return $routes;
     }
     return false;
 }