Esempio n. 1
0
 function handlerServer(Swoole\Request $request)
 {
     $response = new Swoole\Response();
     $request->setGlobal();
     //处理静态请求
     if (!empty($this->server->config['apps']['do_static']) and $this->server->doStaticRequest($request, $response)) {
         return $response;
     }
     $php = Swoole::getInstance();
     //将对象赋值到控制器
     $php->request = $request;
     $php->response = $response;
     try {
         ob_start();
         /*---------------------处理MVC----------------------*/
         $response->body = $php->runMVC();
         $response->body .= ob_get_contents();
         ob_end_clean();
     } catch (\Exception $e) {
         if ($request->finish != 1) {
             $this->server->httpError(404, $response, $e->getMessage());
         }
     }
     //重定向
     if (isset($response->head['Location'])) {
         $response->setHttpStatus(301);
     }
     return $response;
 }
Esempio n. 2
0
 /**
  * 运行MVC处理模型
  */
 function runMVC()
 {
     if (empty($this->request)) {
         $this->request = new \Swoole\Request();
         $this->request->initWithLamp();
     }
     $mvc = call_user_func($this->router_function);
     if ($mvc === false) {
         $this->http->status(404);
         return Swoole\Error::info('MVC Error', "url route fail!");
     }
     //check controller name
     if (!preg_match('/^[a-z0-9_]+$/i', $mvc['controller'])) {
         return Swoole\Error::info('MVC Error!', "controller[{$mvc['controller']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     //check view name
     if (!preg_match('/^[a-z0-9_]+$/i', $mvc['view'])) {
         return Swoole\Error::info('MVC Error!', "view[{$mvc['view']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     //check app name
     if (isset($mvc['app']) and !preg_match('/^[a-z0-9_]+$/i', $mvc['app'])) {
         return Swoole\Error::info('MVC Error!', "app[{$mvc['app']}] name incorrect.Regx: /^[a-z0-9_]+\$/i");
     }
     $this->env['mvc'] = $mvc;
     //使用命名空间,文件名必须大写
     $controller_class = '\\App\\Controller\\' . ucwords($mvc['controller']);
     if (self::$controller_path) {
         $controller_path = self::$controller_path . '/' . ucwords($mvc['controller']) . '.php';
     } else {
         $controller_path = self::$app_path . '/controllers/' . ucwords($mvc['controller']) . '.php';
     }
     if (class_exists($controller_class, false)) {
         goto do_action;
     } else {
         if (is_file($controller_path)) {
             require_once $controller_path;
             goto do_action;
         }
     }
     //file not found
     $this->http->status(404);
     return Swoole\Error::info('MVC Error', "Controller <b>{$mvc['controller']}</b>[{$controller_path}] not exist!");
     do_action:
     //服务器模式下,尝试重载入代码
     if (defined('SWOOLE_SERVER')) {
         $this->reloadController($mvc, $controller_path);
     }
     $controller = new $controller_class($this);
     if (!method_exists($controller, $mvc['view'])) {
         $this->http->status(404);
         return Swoole\Error::info('MVC Error!' . $mvc['view'], "View <b>{$mvc['controller']}->{$mvc['view']}</b> Not Found!");
     }
     $param = empty($mvc['param']) ? null : $mvc['param'];
     $method = $mvc['view'];
     //before action
     $this->callHook(self::HOOK_BEFORE_ACTION);
     //do action
     $return = $controller->{$method}($param);
     //after action
     $this->callHook(self::HOOK_AFTER_ACTION);
     //保存Session
     if (defined('SWOOLE_SERVER') and $this->session->open and $this->session->readonly === false) {
         $this->session->save();
     }
     //响应请求
     if (!empty($controller->is_ajax)) {
         $this->http->header('Cache-Control', 'no-cache, must-revalidate');
         $this->http->header('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
         $this->http->header('Content-Type', 'application/json');
         $return = json_encode($return);
     }
     if (defined('SWOOLE_SERVER')) {
         return $return;
     } else {
         echo $return;
     }
 }