public function __construct(Route $route) { $this->setVars($route->getVars()); $this->setRequest($route->getRequest()); $this->setConfig($route->getConfig()); $this->setName($route->getName()); $this->setIsNotFound($route->isNotFound()); $this->setUriPattern($route->getUriPattern()); foreach ($this->_systemPatterns as $varName => $params) { $value = $route->getVar($varName, $params[0]); if (!empty($params[2])) { $value = call_user_func($params[2], $value); } $this->_systemVars[$varName . 'name'] = str_replace('{name}', $value, $params[1]); } }
public function testBasic() { $route = new Route(); $route->setName('products'); $route->setUriPattern('products/{category}/{id}?'); $route->setConfig(array('controller' => 'IndexController')); $url = $route->buildUri(array('category' => 'macbooks', 'id' => 'air')); $this->assertEquals('products/macbooks/air', $url, 'buildUri works fine'); $this->assertEquals('products/', Route::createInstance('products', 'products/')->buildUri(), 'inline creation'); $this->assertEquals('products', $route, '__toString'); }
/** * @param $uri * @return Route */ public function detectRouteForUri($uri) { $this->_currentRoute = Route::createNotFoundInstance(); $this->_currentRoute->setRequest($this->_currentRequest); if ($uri && $uri[0] !== '/') { $uri = '/' . $uri; } $this->_currentUri = $uri; if (strpos($uri, $this->_webRoot) !== 0) { $this->_currentRoute->setName('webRootNotFound'); return $this->_currentRoute; } foreach ($this->_routes as $routeName => $routeConfig) { $match = UriService::matchPatternToUri($routeConfig['pattern'], $uri, $routeConfig); if ($match) { $this->_currentRoute = new Route($routeName, $routeConfig['pattern'], $routeConfig); $this->_currentRoute->setRequest($this->_currentRequest); $this->_currentRoute->setVars($match); break; } } return $this->_currentRoute; }