public function buildUri($vars = null) { $vars = (array) $vars; foreach ($this->_config as $key => $value) { if (!array_key_exists($key, $vars) && preg_match('#[-_a-z0-9]+#', $value)) { $vars[$key] = $value; } } foreach ($vars as $key => $value) { $this->_vars[$key] = $value; } $uri = UriService::buildUriFromPattern($this->_uriPattern, $this->_vars); return $uri; }
public function testBasic() { $this->assertEquals('#^/$#isU', UriService::buildPattern(null), 'build an empty pattern'); $this->assertEquals('/categories/', UriService::buildInternalPattern('/categories/'), 'build internal category pattern'); $this->assertEquals('#^/categories/$#isU', UriService::buildPattern('/categories/'), 'build category pattern'); $this->assertEquals('/(?P<controller>[-a-z0-9]+)/', UriService::buildInternalPattern('/{controller}/'), 'build controller url'); $this->assertEquals('/(?P<action>[-a-z0-9]+)/', UriService::buildInternalPattern('/{action}/'), 'build action url'); $this->assertEquals('/(?P<action>.*)/', UriService::buildInternalPattern('/{action}/', array('action' => '.*')), 'build custom action url'); $this->assertEquals('/categories/((?P<id>\\d+)/)?', UriService::buildInternalPattern('/categories/({id}/)?', array('id' => '\\d+')), 'build custom parameter url'); $this->assertEquals('/categories/1/products/1', UriService::buildUriFromPattern('/(categories/({id})?)?/products/({id})?', array('id' => 1)), 'build hard url'); $this->assertEquals('1', UriService::buildUriFromPattern('{id}?', array('id' => '1')), 'simple build with "?"'); try { $url = UriService::buildUriFromPattern('{id}'); } catch (\Exception $e) { $this->assertEquals('You have to specify id for pattern', $e->getMessage(), 'Exception if no id specified'); } $this->assertEquals('/products/1/', UriService::buildUriFromPattern('/products/{id}/{action}?', array('id' => 1))); $match = UriService::matchPatternToUri('/categories/({id}/)?', '/categories/1/'); $this->assertEquals(array('id' => 1), $match, 'matcher works'); }
/** * @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; }