/** * Call route loads, process requested url and dispatchs to controllers */ public static function dispatch() { try { self::$url = isset($_GET['url']) ? $_GET['url'] : ''; self::$dispatcher = new Pux\Mux(); self::loadRoutes(); // real dispatcher (Pux) call $route = self::$dispatcher->dispatch('/' . self::$url); // shorthand pointers to processed route variables $vars = is_array($route) && array_key_exists(3, $route) && array_key_exists('vars', $route[3]) ? $route[3]['vars'] : array(); $default = is_array($route) && array_key_exists(3, $route) && array_key_exists('default', $route[3]) ? $route[3]['default'] : array(); // parse controller name from the processed route $controller = ucfirst(!is_null($route[2]) && array_key_exists(0, $route[2]) ? $route[2][0] : (array_key_exists('controller', $vars) ? $vars['controller'] : (array_key_exists('controller', $default) ? $default['controller'] : DEFAULT_CONTROLLER_NAME))); // all controller must have the Controller suffix if (strlen($controller) < 10 || substr($controller, -10) != 'Controller') { $controller .= 'Controller'; } // let's instantiate the controller so if it not exists, should fire // an exception and the rest of the code in this method won't run $Controller = new $controller(); // parse method name from the processed route $method = !is_null($route[2]) && array_key_exists(1, $route[2]) ? $route[2][1] : (array_key_exists('method', $vars) ? $vars['method'] : (array_key_exists('method', $default) ? $default['method'] : DEFAULT_CONTROLLER_METHOD)); // parse arguments to be sent to the controller method from the processed route $args = !is_null($route[2]) && array_key_exists(2, $route[2]) ? $route[2][3] : (!is_null($route[3]) && array_key_exists('args', $vars) ? preg_split('/\\//', $vars['args']) : (!is_null($default) && array_key_exists('args', $default) ? $default['args'] : array())); // there we go call_user_func_array(array($Controller, $method), $args); } catch (Exception $e) { // TO-DO: // probably we couldn't dispatch the request because of // an unreachable controller method so we should show a // 404 page or handle the exception in a proper way error_log('Dispatcher::dispatch: ' . $e->getMessage()); } }
function url($u) { return Dispatcher::url($u); }