|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\response; |
|
14
|
|
|
|
|
15
|
|
|
use think\Request; |
|
16
|
|
|
use think\Response; |
|
17
|
|
|
use think\Session; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Redirect Response |
|
21
|
|
|
*/ |
|
22
|
|
|
class Redirect extends Response |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
protected $request; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Request $request, Session $session, $data = '', int $code = 302) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct((string) $data, $code); |
|
30
|
|
|
|
|
31
|
|
|
$this->request = $request; |
|
32
|
|
|
$this->session = $session; |
|
33
|
|
|
|
|
34
|
|
|
$this->cacheControl('no-cache,must-revalidate'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* 处理数据 |
|
39
|
|
|
* @access protected |
|
40
|
|
|
* @param mixed $data 要处理的数据 |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function output($data): string |
|
44
|
|
|
{ |
|
45
|
|
|
$this->header['Location'] = $data; |
|
46
|
|
|
|
|
47
|
|
|
return ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 重定向传值(通过Session) |
|
52
|
|
|
* @access protected |
|
53
|
|
|
* @param string|array $name 变量名或者数组 |
|
|
|
|
|
|
54
|
|
|
* @param mixed $value 值 |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function with($name, $value = null) |
|
58
|
|
|
{ |
|
59
|
|
|
if (is_array($name)) { |
|
60
|
|
|
foreach ($name as $key => $val) { |
|
61
|
|
|
$this->session->flash($key, $val); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
$this->session->flash($name, $value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* 记住当前url后跳转 |
|
72
|
|
|
* @access public |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function remember() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->session->set('redirect_url', $this->request->url()); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* 跳转到上次记住的url |
|
84
|
|
|
* @access public |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
|
|
public function restore() |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->session->has('redirect_url')) { |
|
|
|
|
|
|
90
|
|
|
$this->data = $this->session->get('redirect_url'); |
|
|
|
|
|
|
91
|
|
|
$this->session->delete('redirect_url'); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|