改进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

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