public static function getId() { $user = static::get(); if (isset($user['id'])) { return $user['id']; } else { Debug::output(new Exception("不存在请求的user_id")); } }
/** * 空方法执行请求方法属性的检查 * 查找相对应的get, post, put, delete等方法并执行 * * @return void */ public function _empty() { try { $method = $this->_method; // 当前http请求方法 $current = strtolower($_SERVER['REQUEST_METHOD']); if (in_array($current, $method)) { $class = $current . '_' . ACTION_NAME; // 是否存在this/get_login if (method_exists($this, $class)) { $this->{$class}(); } else { if (method_exists($this, $current)) { $this->{$current}(); } else { throw new Exception("不存在指定的控制器方法"); } } } else { throw new Exception("不存在'{$method}'方法"); } } catch (Exception $e) { Debug::output($e); } }
/** * 执行controller->action * * @return void */ public static function exec() { // Controller name 安全过滤 if (!preg_match('/^[A-Za-z](\\w)*$/', CONTROLLER_NAME)) { $module = false; } else { $module = Import::controller(GROUP_NAME, CONTROLLER_NAME); } // 执行空控制器 try { if (!$module) { $module = Import::controller(GROUP_NAME, 'Empty'); if (!$module) { throw new Exception("Controller不存在,\"" . CONTROLLER_NAME . "\""); } } } catch (Exception $error) { Debug::output($error); } // 获取控制器操作名 $action = ACTION_NAME; // 定义模板名称 Config::set('TEMPLATE_NAME', THEME_PATH . CONTROLLER_NAME . '/' . $action . '.html'); try { // Action name 安全过滤 if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) { throw new ReflectionException(); } // 对当前控制器的方法执行操作映射 $method = new ReflectionMethod($module, $action); // public方法 if ($method->isPublic()) { // 映射执行 $class = new ReflectionClass($module); // 前置操作 if ($class->hasMethod('_before_' . $action)) { $before = $class->getMethod('_before_' . $action); // public并执行 if ($before->isPublic()) { $before->invoke($module); } } // URL参数绑定检测 if (Config::get('URL_PARAMS_BIND') && $method->getNumberOfParameters() > 0) { switch ($_SERVER['REQUEST_METHOD']) { case 'POST': $vars = $_POST; break; case 'PUT': parse_str(file_get_contents('php://input'), $vars); break; default: $vars = $_GET; } $params = $method->getParameters(); foreach ($params as $param) { $name = $param->getName(); if (isset($vars[$name])) { $args[] = $vars[$name]; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } else { Debug::throw_exception(L('_PARAM_ERROR_') . ':' . $name); } } $method->invokeArgs($module, $args); } else { $method->invoke($module); } // 后置操作 if ($class->hasMethod('_after_' . $action)) { $after = $class->getMethod('_after_' . $action); // public并执行 if ($after->isPublic()) { $after->invoke($module); } } } else { throw new ReflectionException(); } } catch (ReflectionException $e) { // 方法调用发生异常后 引导到__call方法处理 $method = new ReflectionMethod($module, '__call'); $method->invokeArgs($module, array($action, '')); } }
/** * set_category_id获得方式 */ protected function set_category_id() { if ($this->category_id_require && !isset($_GET[$this->category_query_name])) { Debug::output(new Exception('没有提供:' . $this->category_query_name)); } if ($this->category_id_auto_set && isset($_GET[$this->category_query_name])) { $this->category_id = $_GET[$this->category_query_name]; } }
/** * 404处理 * 调试模式会抛异常 * 部署模式下面传入url参数可以指定跳转页面,否则发送404信息 * @param string $msg 提示信息 * @param string $url 跳转URL地址 * @return void */ public static function _404($message = '', $url) { if (APP_DEBUG) { Debug::output(new Exception($message)); } else { static::send_http_status(404); exit; } }