From 544ea65032a71501bade3d50b912ade91b196fef Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 27 Dec 2014 23:21:49 +0200 Subject: [PATCH] controllers and actions --- ipf/controller/base.php | 14 ++++++++++++ ipf/controller/function.php | 10 +++++++++ ipf/middleware/dispatch.php | 4 +++- ipf/router.php | 45 ++++++++++++++++++++++++++++--------- 4 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 ipf/controller/base.php create mode 100644 ipf/controller/function.php diff --git a/ipf/controller/base.php b/ipf/controller/base.php new file mode 100644 index 0000000..669d6d9 --- /dev/null +++ b/ipf/controller/base.php @@ -0,0 +1,14 @@ +request = $request; + $this->params = $matches; + return $this->$action(); + } +} + diff --git a/ipf/controller/function.php b/ipf/controller/function.php new file mode 100644 index 0000000..0344c28 --- /dev/null +++ b/ipf/controller/function.php @@ -0,0 +1,10 @@ +params = $match; try { - return IPF::callFunction($route->func, array($request, $match)); + $controller = $route->controller(); + $controller = new $controller; + return $controller->process($route->action(), $request, $match); } catch (IPF_Router_Shortcut $e) { return $e->response($request); } diff --git a/ipf/router.php b/ipf/router.php index 99be41b..5e1347c 100644 --- a/ipf/router.php +++ b/ipf/router.php @@ -9,15 +9,19 @@ class IPF_Router $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'); } @@ -50,9 +54,9 @@ class IPF_Router 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.'); } } @@ -116,13 +120,13 @@ class IPF_Router_RegexMatch 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() @@ -133,6 +137,27 @@ class IPF_Route } 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 -- 2.49.0