Session类增加flash方法用于设置下一次请求有效的值 flush方法用于清空当前请求有效的值 增加push方法

Redirect response类的with方法改为调用session类的flash方法
This commit is contained in:
thinkphp
2016-10-19 15:16:31 +08:00
parent 53233db821
commit a25b16b0cf
3 changed files with 61 additions and 4 deletions

View File

@@ -130,6 +130,9 @@ class Response
// 监听response_end
Hook::listen('response_end', $this);
// 清空当次请求有效的数据
Session::flush();
}
/**

View File

@@ -196,8 +196,42 @@ class Session
}
/**
* 删除session数据
* session设置 下一次请求有效
* @param string $name session名称
* @param mixed $value session值
* @param string|null $prefix 作用域(前缀)
* @return void
*/
public static function flash($name, $value)
{
self::set($name, $value);
if (!self::has('__flash__.__time__')) {
self::set('__flash__.__time__', $_SERVER['REQUEST_TIME_FLOAT']);
}
self::push('__flash__', $name);
}
/**
* 清空当前请求的session数据
* @return void
*/
public static function flush()
{
$item = self::get('__flash__');
if (!empty($item)) {
$time = $item['__time__'];
if ($_SERVER['REQUEST_TIME_FLOAT'] > $time) {
unset($item['__time__']);
self::delete($item);
self::set('__flash__', []);
}
}
}
/**
* 删除session数据
* @param string|array $name session名称
* @param string|null $prefix 作用域(前缀)
* @return void
*/
@@ -205,7 +239,11 @@ class Session
{
empty(self::$init) && self::boot();
$prefix = !is_null($prefix) ? $prefix : self::$prefix;
if (strpos($name, '.')) {
if (is_array($name)) {
foreach ($name as $key) {
self::delete($key, $prefix);
}
} elseif (strpos($name, '.')) {
list($name1, $name2) = explode('.', $name);
if ($prefix) {
unset($_SESSION[$prefix][$name1][$name2]);
@@ -256,6 +294,22 @@ class Session
}
}
/**
* 添加数据到一个session数组
* @param string $key
* @param mixed $value
* @return void
*/
public static function push($key, $value)
{
$array = self::get($key);
if (is_null($array)) {
$array = [];
}
$array[] = $value;
self::set($key, $array);
}
/**
* 启动session
* @return void

View File

@@ -53,10 +53,10 @@ class Redirect extends Response
{
if (is_array($name)) {
foreach ($name as $key => $val) {
Session::set($key, $val);
Session::flash($key, $val);
}
} else {
Session::set($name, $value);
Session::flash($name, $value);
}
return $this;
}