改进Cookie类的自动初始化 Controller类增加_initialize方法

This commit is contained in:
thinkphp
2016-07-28 16:19:58 +08:00
parent 8853b58838
commit 1b6622fd36
2 changed files with 15 additions and 5 deletions

View File

@@ -50,9 +50,7 @@ class Controller
$this->request = $request; $this->request = $request;
// 控制器初始化 // 控制器初始化
if (method_exists($this, '_initialize')) {
$this->_initialize(); $this->_initialize();
}
// 前置操作方法 // 前置操作方法
if ($this->beforeActionList) { if ($this->beforeActionList) {
@@ -64,6 +62,11 @@ class Controller
} }
} }
// 初始化
protected function _initialize()
{
}
/** /**
* 前置操作 * 前置操作
* @access protected * @access protected

View File

@@ -30,6 +30,8 @@ class Cookie
'setcookie' => true, 'setcookie' => true,
]; ];
protected static $init;
/** /**
* Cookie初始化 * Cookie初始化
* @param array $config * @param array $config
@@ -44,6 +46,7 @@ class Cookie
if (!empty(self::$config['httponly'])) { if (!empty(self::$config['httponly'])) {
ini_set('session.cookie_httponly', 1); ini_set('session.cookie_httponly', 1);
} }
self::$init = true;
} }
/** /**
@@ -71,6 +74,7 @@ class Cookie
*/ */
public static function set($name, $value = '', $option = null) public static function set($name, $value = '', $option = null)
{ {
!isset(self::$init) && self::init();
// 参数设置(会覆盖黙认设置) // 参数设置(会覆盖黙认设置)
if (!is_null($option)) { if (!is_null($option)) {
if (is_numeric($option)) { if (is_numeric($option)) {
@@ -103,6 +107,7 @@ class Cookie
*/ */
public static function has($name, $prefix = null) public static function has($name, $prefix = null)
{ {
!isset(self::$init) && self::init();
$prefix = !is_null($prefix) ? $prefix : self::$config['prefix']; $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
$name = $prefix . $name; $name = $prefix . $name;
return isset($_COOKIE[$name]); return isset($_COOKIE[$name]);
@@ -116,6 +121,7 @@ class Cookie
*/ */
public static function get($name, $prefix = null) public static function get($name, $prefix = null)
{ {
!isset(self::$init) && self::init();
$prefix = !is_null($prefix) ? $prefix : self::$config['prefix']; $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
$name = $prefix . $name; $name = $prefix . $name;
if (isset($_COOKIE[$name])) { if (isset($_COOKIE[$name])) {
@@ -139,6 +145,7 @@ class Cookie
*/ */
public static function delete($name, $prefix = null) public static function delete($name, $prefix = null)
{ {
!isset(self::$init) && self::init();
$config = self::$config; $config = self::$config;
$prefix = !is_null($prefix) ? $prefix : $config['prefix']; $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
$name = $prefix . $name; $name = $prefix . $name;
@@ -160,7 +167,7 @@ class Cookie
if (empty($_COOKIE)) { if (empty($_COOKIE)) {
return; return;
} }
!isset(self::$init) && self::init();
// 要删除的cookie前缀不指定则删除config设置的指定前缀 // 要删除的cookie前缀不指定则删除config设置的指定前缀
$config = self::$config; $config = self::$config;
$prefix = !is_null($prefix) ? $prefix : $config['prefix']; $prefix = !is_null($prefix) ? $prefix : $config['prefix'];