83 $this->routes = array();
108 $this->request = $req;
116 if ( $reload || is_null($this->matchedRoutes) ) {
117 $this->matchedRoutes = array();
118 foreach ( $this->routes as $route ) {
119 if ( $route->matches($this->request->getResourceUri()) ) {
120 $this->matchedRoutes[] = $route;
133 public function map( $pattern, $callable ) {
135 $route->setRouter($this);
136 $this->routes[] = $route;
148 if ( isset($this->namedRoutes[(
string)$name]) ) {
149 throw new RuntimeException(
'Named route already exists with name: ' . $name);
151 $this->namedRoutes[$name] = $route;
161 public function urlFor( $name, $params = array() ) {
162 if ( !isset($this->namedRoutes[(
string)$name]) ) {
163 throw new RuntimeException(
'Named route not found for name: ' . $name);
165 $pattern = $this->namedRoutes[(string)$name]->getPattern();
166 $search = $replace = array();
167 foreach ( $params as $key => $value ) {
168 $search[] =
':' . $key;
171 $pattern = str_replace($search, $replace, $pattern);
173 return preg_replace(array(
174 '@\(\/?:.+\/??\)\??@',
176 ),
'', $this->request->getRootUri() . $pattern);
185 if ( is_callable($callable) ) {
196 public function error( $callable = null ) {
197 if ( is_callable($callable) ) {
198 $this->
error = $callable;