public function invoke(URLComponent $url) { $class_name = $url->getController(); $method = $url->getAction(); $class = $this->app . '\\action\\' . $class_name; //Response对象 $response = Response::getInstance(); //Request对象 $request = Request::getInstance(); #实例化控制器,使用反射 $reflection = new \ReflectionClass($class); $instacne = $reflection->newInstance(); //先执行初始化方法init if ($reflection->hasMethod('init')) { $init = $reflection->getMethod('init'); $data = $init->invokeArgs($instacne, array($request, $response)); if ($data) { //如果有返回数据则输出 $response->setBody($data); $response->send(); return true; } } if ($reflection->hasMethod($method)) { $method = $reflection->getMethod($method); } elseif ($reflection->hasMethod('getMiss')) { $method = $reflection->getMethod('getMiss'); } else { throw new RouteException('Method does not exist.'); } $data = $method->invokeArgs($instacne, array($request, $response)); #输出 $response->setBody($data); $response->send(); }
/** * 解析命令 * * @param string $controller 控制器 * @param string $action 方法 * @return URLComponent */ public static function parseController($controller, $action) { $request = Request::getInstance(); $controller = strtolower($controller); $controller = ctype_alpha($controller) ? $controller : 'index'; $controller = ucfirst($controller); $action = str_replace(array('_', '-'), ' ', $action); $action = ucwords($action); $action = strtolower($action); $action = ucfirst($action); $action = str_replace(' ', '', $action); $action = ctype_alpha($action) ? $action : 'Index'; if (!$request->isPost()) { $action = 'get' . $action; } elseif ($request->isPost()) { $action = 'post' . $action; } $Url = new URLComponent(); $Url->setController($controller)->setAction($action); return $Url; }