| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class RedirectResponse extends Response |
| 20 |
{ |
| 21 |
protected string $targetUrl; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function __construct(string $url, int $status = 302, array $headers = []) |
| 36 |
{ |
| 37 |
parent::__construct('', $status, $headers); |
| 38 |
|
| 39 |
$this->setTargetUrl($url); |
| 40 |
|
| 41 |
if (!$this->isRedirect()) { |
| 42 |
throw new \InvalidArgumentException(\sprintf('The HTTP status code is not a redirect ("%s" given).', $status)); |
| 43 |
} |
| 44 |
|
| 45 |
if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) { |
| 46 |
$this->headers->remove('cache-control'); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function getTargetUrl(): string |
| 54 |
{ |
| 55 |
return $this->targetUrl; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function setTargetUrl(string $url): static |
| 66 |
{ |
| 67 |
if ('' === $url) { |
| 68 |
throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); |
| 69 |
} |
| 70 |
|
| 71 |
$this->targetUrl = $url; |
| 72 |
|
| 73 |
$this->setContent( |
| 74 |
\sprintf('<!DOCTYPE html> |
| 75 |
<html> |
| 76 |
<head> |
| 77 |
<meta charset="UTF-8" /> |
| 78 |
<meta http-equiv="refresh" content="0;url=\'%1$s\'" /> |
| 79 |
|
| 80 |
<title>Redirecting to %1$s</title> |
| 81 |
</head> |
| 82 |
<body> |
| 83 |
Redirecting to <a href="%1$s">%1$s</a>. |
| 84 |
</body> |
| 85 |
</html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8'))); |
| 86 |
|
| 87 |
$this->headers->set('Location', $url); |
| 88 |
$this->headers->set('Content-Type', 'text/html; charset=utf-8'); |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
} |