/** * Compares the uri string against the routes that the * RouteCollection class defined for us, attempting to find a match. * This method will modify $this->controller, etal as needed. * * @param string $uri The URI path to compare against the routes * * @return bool Whether the route was matched or not. */ protected function checkRoutes(string $uri) : bool { $routes = $this->collection->getRoutes(); // Don't waste any time if (empty($routes)) { return false; } // Loop through the route array looking for wildcards foreach ($routes as $key => $val) { // Does the RegEx match? if (preg_match('#^' . $key . '$#', $uri, $matches)) { // Are we using Closures? If so, then we need // to collect the params into an array // so it can be passed to the controller method later. if (!is_string($val) && is_callable($val)) { $this->controller = $val; // Remove the original string from the matches array array_shift($matches); $this->params = $matches; return true; } elseif (strpos($val, '$') !== false && strpos($key, '(') !== false) { $val = preg_replace('#^' . $key . '$#', $val, $uri); } $this->setRequest(explode('/', $val)); return true; } } return false; }
public function addCollection(RouteCollectionInterface $collection) { foreach ($collection->getRoutes() as $route) { $this->addRoute($route); } return $this; }