$this->flattenRoutes(IPF::get('urls'));
}
- private function flattenRoutes($routes, $prefix='')
+ private function flattenRoutes($routes, $prefix='', $common=array())
{
foreach ($routes as $route) {
- if (isset($route['prefix'])) {
- $this->flattenRoutes($route['urls'], $prefix . $route['prefix']);
+ $route = array_merge($common, $route);
+
+ if (isset($route['urls'])) {
+ $nested = \PFF\Arr::pop($route, 'urls');
+ $prefix .= \PFF\Arr::pop($route, 'prefix', '');
+ $this->flattenRoutes($nested, $prefix, $route);
} elseif (isset($route['regex'])) {
- $this->routes[] = new IPF_Route($prefix . $route['regex'], 'IPF_Router_RegexMatch', $route['func']);
+ $this->routes[] = new IPF_Route($prefix . $route['regex'], 'IPF_Router_RegexMatch', $route);
} elseif (isset($route['expr'])) {
- $this->routes[] = new IPF_Route($prefix . $route['expr'], 'RouteExpression', $route['func']);
+ $this->routes[] = new IPF_Route($prefix . $route['expr'], 'RouteExpression', $route);
} else {
throw new IPF_Exception('Unsupported route type');
}
public function reverse($view, $params=array())
{
foreach ($this->routes as $route)
- if ($route->func == $view)
+ if ($route->routesTo($view))
return IPF::get('app_base') . $route->matcher()->reverse($params);
- throw new IPF_Exception('Error, the view: '.$view.' has not been found.');
+ throw new IPF_Exception('Error, the view: '.print_r($view, true).' has not been found.');
}
}
class IPF_Route
{
private $matcher;
- public $expr, $func;
+ public $expr, $params;
- public function __construct($expr, $matcher, $func)
+ public function __construct($expr, $matcher, $params)
{
$this->expr = $expr;
$this->matcher = $matcher;
- $this->func = $func;
+ $this->params = $params;
}
public function matcher()
}
return $this->matcher;
}
+
+ public function controller()
+ {
+ return \PFF\Arr::get($this->params, 'controller', 'IPF_Controller_Function');
+ }
+
+ public function action()
+ {
+ return \PFF\Arr::get($this->params, 'func', 'index');
+ }
+
+ public function routesTo($view)
+ {
+ if (is_array($view)) {
+ list($controller, $action) = $view;
+ } else {
+ $controller = 'IPF_Controller_Function';
+ $action = $view;
+ }
+ return $controller === $this->controller() && $action === $this->action();
+ }
}
abstract class IPF_Router_Shortcut extends Exception