-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathRedirect.php
More file actions
166 lines (143 loc) · 3.13 KB
/
Copy pathRedirect.php
File metadata and controls
166 lines (143 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace Bow\Http;
use Bow\Contracts\ResponseInterface;
class Redirect implements ResponseInterface
{
/**
* The Redirect instance
*
* @var ?Redirect
*/
private static ?Redirect $instance = null;
/**
* The Request instance
*
* @var Request
*/
private Request $request;
/**
* The redirect targets
*
* @var string
*/
private string $to;
/**
* The Response instance
*
* @var Response
*/
private Response $response;
/**
* Redirect constructor.
*
* @return void
*/
private function __construct()
{
$this->request = Request::getInstance();
$this->response = Response::getInstance();
}
/**
* Get redirection instance
*
* @return Redirect
*/
public static function getInstance(): Redirect
{
if (!static::$instance) {
static::$instance = new Redirect();
}
return static::$instance;
}
/**
* Redirection with the query information
*
* @param array $data
* @return Redirect
*/
public function withInput(array $data = []): Redirect
{
if (count($data) == 0) {
$this->request->session()->add('__bow.old', $this->request->all());
} else {
$this->request->session()->add('__bow.old', $data);
}
return $this;
}
/**
* Redirection with define flash information
*
* @param string $key
* @param mixed $value
* @return Redirect
*/
public function withFlash(string $key, mixed $value): Redirect
{
$this->request->session()->flash($key, $value);
return $this;
}
/**
* Redirect with route definition
*
* @param string $name
* @param array $data
* @param bool $absolute
* @return Redirect
*/
public function route(string $name, array $data = [], bool $absolute = false): Redirect
{
$this->to = route($name, $data, $absolute);
return $this;
}
/**
* Redirect on the previous URL
*
* @param int $status
* @return Redirect
*/
public function back(int $status = 302): Redirect
{
$this->to($this->request->referer(), $status);
return $this;
}
/**
* Redirect to another URL
*
* @param string $path
* @param int $status
* @return Redirect
*/
public function to(string $path, int $status = 302): Redirect
{
$this->to = $path;
$this->response->status($status);
return $this;
}
/**
* @inheritdoc
*/
public function sendContent(): void
{
$this->response->withHeader('Location', $this->to);
$this->response->sendContent();
}
/**
* __invoke
*
* @return mixed
*/
public function __invoke(): mixed
{
return call_user_func_array([$this, 'to'], func_get_args());
}
/**
* __toString
*
* @return string
*/
public function __toString(): string
{
return $this->to;
}
}