/** * get * * @param mixed $name * @param mixed $default */ public static function get($name = null, $default = null) { if (is_null($name)) { return Request::cookies()->all(); } else { return Router::getInstance()->getCookie($name) ?: $default; } }
/** * Cookie相关操作 * @param string $name * @param string $value * @param string $opt * @return string|null */ public static function cookie($name = '', $value = '', $opt = null) { //参数设置处理 if (!is_null($opt)) { if (is_numeric($opt)) { $opt = array('expire' => $opt); //设置有效期 } self::$_cookieConfig = array_merge(self::$_cookieConfig, $opt); } //清除所有Cookie if (is_null($name)) { $cookies = Request::cookies(); if (!empty($cookies)) { foreach ($cookies as $key => $val) { setcookie($key, '', time() - 3600, self::$_cookieConfig['path'], self::$_cookieConfig['domain'], self::$_cookieConfig['secure'], self::$_cookieConfig['httponly']); Request::unsetParam($key, 'cookie'); } } return null; } elseif ('' === $name) { return Request::cookies(); //获取所有cookie } $cookieKey = self::$_cookieConfig['prefix'] . $name; if ('' === $value) { //获取指定的Cookie if (Request::cookie($cookieKey)) { return Request::cookie($cookieKey); } return null; } else { if (is_null($value)) { //删除指定Cookie setcookie($cookieKey, '', time() - 3600, self::$_cookieConfig['path'], self::$_cookieConfig['domain'], self::$_cookieConfig['secure'], self::$_cookieConfig['httponly']); Request::unsetParam($cookieKey, 'cookie'); } else { //设置Cookie $expire = !empty(self::$_cookieConfig['expire']) ? time() + intval(self::$_cookieConfig['expire']) : 0; setcookie($cookieKey, $value, $expire, self::$_cookieConfig['path'], self::$_cookieConfig['domain'], self::$_cookieConfig['secure'], self::$_cookieConfig['httponly']); } } return null; }
private static function _exec() { define('CURRENT_MODULE', Dispatcher::getModuleName()); define('CURRENT_CONTROLLER', Dispatcher::getControllerName()); define('CURRENT_ACTION', Dispatcher::getActionName()); $controllerName = CURRENT_CONTROLLER; $moduleName = CURRENT_MODULE; if ($moduleName) { $className = sprintf('controller\\%s\\%s', $moduleName, $controllerName); } else { $className = sprintf("controller\\%s", $controllerName); } //-----请求日志------// $params = array(); $log = array('request_id' => REQUEST_ID, 'uri' => $_SERVER['REQUEST_URI'], 'class' => array('module' => CURRENT_MODULE, 'controller' => CURRENT_CONTROLLER, 'action' => CURRENT_ACTION), 'method' => Request::method(), 'params' => array_merge($params, Request::gets(), Request::posts()), 'stream' => Request::stream(), 'cookie' => Request::cookies(), 'ip' => Request::ip()); C::log($log); if (!class_exists($className)) { throw new \RuntimeException('类不存在'); } try { $obj = new \ReflectionClass($className); if ($obj->isAbstract()) { throw new \RuntimeException('抽象类不可被实例化'); } $class = $obj->newInstance(); //前置操作 if ($obj->hasMethod(CURRENT_ACTION . 'Before')) { $beforeMethod = $obj->getMethod(CURRENT_ACTION . 'Before'); if ($beforeMethod->isPublic() && !$beforeMethod->isStatic()) { $beforeMethod->invoke($class); } } $method = $obj->getMethod(CURRENT_ACTION . 'Action'); if ($method->isPublic() && !$method->isStatic()) { $method->invoke($class); } //后置操作 if ($obj->hasMethod(CURRENT_ACTION . 'After')) { $afterMethod = $obj->getMethod(CURRENT_ACTION . 'After'); if ($afterMethod->isPublic() && !$afterMethod->isStatic()) { $afterMethod->invoke($class); } } } catch (\Exception $e) { self::_halt($e); } }