function detail() { session(); if (empty($_GET['aid'])) { exit; } $_user = createModel('UserInfo'); $_reply = createModel('AskReply'); $aid = (int) $_GET['aid']; $ask = createModel('AskSubject')->get($aid); if (empty($ask->_data)) { Swoole\Http::header("HTTP/1.1 404 Not Found"); return Swoole\Error::info("Page not found", ""); } $ask->lcount++; $ask->save(); $timeout['day'] = intval(($ask['expire'] - time()) / 86400); $timeout['hour'] = intval(($ask['expire'] - time() - $timeout['day'] * 86400) / 3600); $user = $_user->get($ask['uid'])->get(); $content = createModel('AskContent')->get($aid)->get(); $gets['aid'] = $aid; $gets['select'] = $_reply->table . '.id as id,uid,sex,best,content,nickname,avatar,addtime'; $gets['order'] = 'best desc,' . $_reply->table . '.id asc'; $gets['leftjoin'] = array($_user->table, $_user->table . '.id=' . $_reply->table . '.uid'); $gets['pagesize'] = 10; $gets['page'] = empty($_GET['page']) ? 1 : (int) $_GET['page']; $replys = $_reply->gets($gets, $pager); $if_vote = true; if ($_SESSION['isLogin']) { $vote = $this->swoole->db->query("select count(*) as c from ask_vote where aid={$aid} and uid={$_SESSION['user_id']} limit 1")->fetch(); if ($vote['c'] > 0) { $if_vote = false; } } $this->swoole->tpl->assign('if_vote', $if_vote); $this->swoole->tpl->assign('if_vote', $if_vote); $this->swoole->tpl->assign('expire', $timeout); $this->swoole->tpl->ref('user', $user); $this->swoole->tpl->ref('ask', $ask->get()); $this->swoole->tpl->ref('content', $content); $this->swoole->tpl->ref('replys', $replys); if ($pager->totalpage > 1) { $this->swoole->tpl->ref('pager', $pager->render()); } $this->swoole->tpl->display(); }
/** * 错误信息输出处理 */ function swoole_error_handler($errno, $errstr, $errfile, $errline) { $info = ''; switch ($errno) { case E_USER_ERROR: $level = 'User Error'; break; case E_USER_WARNING: $level = 'Warnning'; break; case E_USER_NOTICE: $level = 'Notice'; break; default: $level = 'Unknow'; break; } $title = 'Swoole ' . $level; $info .= '<b>File:</b> ' . $errfile . "<br />\n"; $info .= '<b>Line:</b> ' . $errline . "<br />\n"; $info .= '<b>Info:</b> ' . $errstr . "<br />\n"; $info .= '<b>Code:</b> ' . $errno . "<br />\n"; echo Swoole\Error::info($title, $info); }
<?php if (!defined('EVENT_MODE') or EVENT_MODE == 'sync') { $event = new SwooleEvent('sync'); } else { $queue_url = ''; $queue_type = 'CacheQueue'; if (defined('EVENT_QUEUE')) { $queue_url = EVENT_QUEUE; } if (defined('EVENT_QUEUE_TYPE')) { $queue_type = EVENT_QUEUE_TYPE; } $event = new SwooleEvent('async', $queue_url, $queue_type); } if (defined('EVENT_HANDLE')) { require EVENT_HANDLE; if (empty($handle)) { Swoole\Error::info('SwooleEvent Error', 'Event handles not be empty!'); } $event->set_listens($handle); } return $event;
/** * 运行MVC处理模型 * @param $url_processor * @return None */ function runMVC() { $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']); $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 (!is_callable(array($controller, $mvc['view']))) { $this->http->status(404); return Swoole\Error::info('MVC Error!' . $mvc['view'], "View <b>{$mvc['controller']}->{$mvc['view']}</b> Not Found!"); } if (empty($mvc['param'])) { $param = null; } else { $param = $mvc['param']; } $method = $mvc['view']; //doAction $return = $controller->{$method}($param); //保存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; } }
/** * 运行MVC处理模型 * @param $url_processor * @return None */ function runMVC() { $mvc = $this->urlRoute(); if ($mvc === false) { \Swoole\Http::status(404); return Swoole\Error::info('MVC Error', "url route fail!"); } //check 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"); } 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"); } 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_path = self::$app_path . "/controllers/{$mvc['controller']}.php"; if (!class_exists($mvc['controller'], false)) { if (!is_file($controller_path)) { \Swoole\Http::status(404); return Swoole\Error::info('MVC Error', "Controller <b>{$mvc['controller']}</b> not exist!"); } else { require_once $controller_path; } } //服务器模式下,尝试重载入代码 if (defined('SWOOLE_SERVER')) { $this->reloadController($mvc, $controller_path); } $class = $mvc['controller']; $controller = new $class($this); if (!is_callable(array($controller, $mvc['view']))) { \Swoole\Http::status(404); return Swoole\Error::info('MVC Error!' . $mvc['view'], "View <b>{$mvc['controller']}->{$mvc['view']}</b> Not Found!"); } if (empty($mvc['param'])) { $param = null; } else { $param = $mvc['param']; } $method = $mvc['view']; $return = $controller->{$method}($param); //保存Session if ($this->session->open and $this->session->readonly === false) { $this->session->save(); } //响应请求 if ($controller->is_ajax) { header('Cache-Control: no-cache, must-revalidate'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Content-type: application/json'); $return = json_encode($return); } if (defined('SWOOLE_SERVER')) { return $return; } else { echo $return; } }