/** * Dispatch all routes and perform the appropriate controller (in order to run!) */ public function dispatch() { $this->checkBaseURI(); // Prepare all routes of the requested method if (!isset($this->routes[null]) || !is_array($this->routes[null])) { $this->routes[null] = array(); } if (!isset($this->routes[$this->request->getMethod()]) || !is_array($this->routes[$this->request->getMethod()])) { $this->routes[$this->request->getMethod()] = array(); } $routes = array_merge($this->routes[null], $this->routes[$this->request->getMethod()]); // Flag down! Not found is the default $flag = false; // Check all routes foreach ($routes as $route => $route_options) { // Extract all the current route parameter $route_pattern = $this->convertToRegex($route); // Check if the request page is match with current route if (preg_match($route_pattern, $this->request->getLocalUri(), $arguments)) { // Check the domain $domain_args = $this->checkDomain($route_options["domain"]); // Flag up! found, matches... $flag = true; $arguments["request"] = isset($parameters["request"]) ? $parameters["request"] : $this->request; $arguments["response"] = isset($parameters["response"]) ? $parameters["response"] : $this->response; $parameters = array_merge($domain_args, $arguments); // Controller... if (is_callable($controller = $route_options["controller"])) { $parameters = $this->arrangeFuncArgs($controller, $parameters); } else { $list = explode('@', $controller = $route_options["controller"]); if (count($list) != 2) { throw new BadController('Cannot detect the controller class'); } if (method_exists($class = $list[0], $method = $list[1])) { $c = new $class(); $parameters = $this->arrangeMethodArgs($c, $method, $parameters); $controller = array($c, $method); } else { throw new BadController('Cannot detect the controller method'); } } // Run middleware if exists if (!empty($route_options["middleware"])) { foreach ($route_options["middleware"] as $middleware) { if (is_callable($middleware) || function_exists($middleware)) { $mid_args = array_merge($domain_args, $arguments); $mid_args = $this->arrangeFuncArgs($middleware, $mid_args); $this->publish(call_user_func_array($middleware, $mid_args)); } else { if (is_string($middleware)) { $list = explode('@', $middleware); if (count($list) != 2) { throw new BadMiddleware('Cannot detect the middleware class'); } if (method_exists($list[0], $list[1])) { $mid_args = $arguments; $mid_args = $this->arrangeMethodArgs($list[0], $list[1], $mid_args); $mid_instance = new $list[0](); $this->publish(call_user_func_array(array($mid_instance, $list[1]), $mid_args)); } else { throw new BadMiddleware('Cannot detect the middleware method'); } } } } } // Run controller function or method if (is_callable($controller)) { $this->publish(call_user_func_array($controller, $parameters)); } else { if (is_array($controller)) { $this->publish(call_user_func_array($controller[0]->{$controller}[1], $parameters)); } } } } if (!$flag) { throw new HttpError(404); } }