Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpFoundation;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * RedirectResponse represents an HTTP response doing a redirect.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class RedirectResponse extends Response
|
Chris@0
|
20 {
|
Chris@0
|
21 protected $targetUrl;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Creates a redirect response so that it conforms to the rules defined for a redirect status code.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @param string $url The URL to redirect to. The URL should be a full URL, with schema etc.,
|
Chris@0
|
27 * but practically every browser redirects on paths only as well
|
Chris@0
|
28 * @param int $status The status code (302 by default)
|
Chris@0
|
29 * @param array $headers The headers (Location is always set to the given URL)
|
Chris@0
|
30 *
|
Chris@0
|
31 * @throws \InvalidArgumentException
|
Chris@0
|
32 *
|
Chris@0
|
33 * @see http://tools.ietf.org/html/rfc2616#section-10.3
|
Chris@0
|
34 */
|
Chris@0
|
35 public function __construct($url, $status = 302, $headers = array())
|
Chris@0
|
36 {
|
Chris@0
|
37 parent::__construct('', $status, $headers);
|
Chris@0
|
38
|
Chris@0
|
39 $this->setTargetUrl($url);
|
Chris@0
|
40
|
Chris@0
|
41 if (!$this->isRedirect()) {
|
Chris@0
|
42 throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 if (301 == $status && !array_key_exists('cache-control', $headers)) {
|
Chris@0
|
46 $this->headers->remove('cache-control');
|
Chris@0
|
47 }
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 public static function create($url = '', $status = 302, $headers = array())
|
Chris@0
|
54 {
|
Chris@0
|
55 return new static($url, $status, $headers);
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Returns the target URL.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return string target URL
|
Chris@0
|
62 */
|
Chris@0
|
63 public function getTargetUrl()
|
Chris@0
|
64 {
|
Chris@0
|
65 return $this->targetUrl;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Sets the redirect target of this response.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @param string $url The URL to redirect to
|
Chris@0
|
72 *
|
Chris@0
|
73 * @return $this
|
Chris@0
|
74 *
|
Chris@0
|
75 * @throws \InvalidArgumentException
|
Chris@0
|
76 */
|
Chris@0
|
77 public function setTargetUrl($url)
|
Chris@0
|
78 {
|
Chris@0
|
79 if (empty($url)) {
|
Chris@0
|
80 throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 $this->targetUrl = $url;
|
Chris@0
|
84
|
Chris@0
|
85 $this->setContent(
|
Chris@0
|
86 sprintf('<!DOCTYPE html>
|
Chris@0
|
87 <html>
|
Chris@0
|
88 <head>
|
Chris@0
|
89 <meta charset="UTF-8" />
|
Chris@0
|
90 <meta http-equiv="refresh" content="1;url=%1$s" />
|
Chris@0
|
91
|
Chris@0
|
92 <title>Redirecting to %1$s</title>
|
Chris@0
|
93 </head>
|
Chris@0
|
94 <body>
|
Chris@0
|
95 Redirecting to <a href="%1$s">%1$s</a>.
|
Chris@0
|
96 </body>
|
Chris@0
|
97 </html>', htmlspecialchars($url, ENT_QUOTES, 'UTF-8')));
|
Chris@0
|
98
|
Chris@0
|
99 $this->headers->set('Location', $url);
|
Chris@0
|
100
|
Chris@0
|
101 return $this;
|
Chris@0
|
102 }
|
Chris@0
|
103 }
|