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