function run() { $this->_setServices(); $route = Service::get('router')->parseRoute(); try { if (!empty($route)) { $controllerReflection = new \ReflectionClass($route['controller']); if (!$controllerReflection->isSubclassOf('Framework\\Controller\\Controller')) { throw new \Exception("Unknown controller " . $controllerReflection->name); } $action = $route['action'] . 'Action'; if ($controllerReflection->hasMethod($action)) { // ReflectionMethod::invokeArgs() has overloaded in class ReflectionMethodNamedArgs // Now it provides invoking with named arguments $actionReflection = new ReflectionMethodNamedArgs($route['controller'], $action); $controller = $controllerReflection->newInstance(); $response = $actionReflection->invokeArgs($controller, $route['parameters']); if ($response instanceof Response) { // ... } else { throw new BadResponseTypeException('Response type not known'); } } } else { throw new HttpNotFoundException('Route not found'); } } catch (HttpNotFoundException $e) { $code = (string) $e->getCode(); $response = $this->render($code . '.html', array('code' => $code, 'message' => $e->getMessage())); // Render 404 } catch (AuthRequredException $e) { $response = new ResponseRedirect($this->generateRoute('login')); // Reroute to login page } catch (\Exception $e) { $code = '500'; $response = $this->render($code . '.html', array('code' => (string) $e->getCode(), 'message' => $e->getMessage())); // Do 500 layout... } $response->send(); }
/** * Widget for view template * * @param string $controller_name * @param string $action * @param array $data * * @throws \Exception If obtained controller is not subclass of base controller. */ private function _widget($controller_name, $action, $data = array()) { $controllerReflection = new \ReflectionClass($controller_name); if (!$controllerReflection->isSubclassOf('Framework\\Controller\\Controller')) { throw new \Exception("Unknown controller " . $controllerReflection->name); } $action .= 'Action'; if ($controllerReflection->hasMethod($action)) { // ReflectionMethod::invokeArgs() has overloaded in class ReflectionMethodNamedArgs // Now it provides invoking with named arguments $actionReflection = new ReflectionMethodNamedArgs($controller_name, $action); $controller = $controllerReflection->newInstance(); $response = $actionReflection->invokeArgs($controller, $data); if ($response instanceof Response) { echo htmlspecialchars_decode($response['content']); } else { throw new BadResponseTypeException('Response type not known'); } } }