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