annotate vendor/symfony/http-foundation/RedirectResponse.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
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@17 35 public function __construct($url, $status = 302, $headers = [])
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@18 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@14 51 * Factory method for chainability.
Chris@14 52 *
Chris@14 53 * @param string $url The url to redirect to
Chris@14 54 * @param int $status The response status code
Chris@14 55 * @param array $headers An array of response headers
Chris@14 56 *
Chris@14 57 * @return static
Chris@0 58 */
Chris@17 59 public static function create($url = '', $status = 302, $headers = [])
Chris@0 60 {
Chris@0 61 return new static($url, $status, $headers);
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Returns the target URL.
Chris@0 66 *
Chris@0 67 * @return string target URL
Chris@0 68 */
Chris@0 69 public function getTargetUrl()
Chris@0 70 {
Chris@0 71 return $this->targetUrl;
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Sets the redirect target of this response.
Chris@0 76 *
Chris@0 77 * @param string $url The URL to redirect to
Chris@0 78 *
Chris@0 79 * @return $this
Chris@0 80 *
Chris@0 81 * @throws \InvalidArgumentException
Chris@0 82 */
Chris@0 83 public function setTargetUrl($url)
Chris@0 84 {
Chris@0 85 if (empty($url)) {
Chris@0 86 throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
Chris@0 87 }
Chris@0 88
Chris@0 89 $this->targetUrl = $url;
Chris@0 90
Chris@0 91 $this->setContent(
Chris@0 92 sprintf('<!DOCTYPE html>
Chris@0 93 <html>
Chris@0 94 <head>
Chris@0 95 <meta charset="UTF-8" />
Chris@12 96 <meta http-equiv="refresh" content="0;url=%1$s" />
Chris@0 97
Chris@0 98 <title>Redirecting to %1$s</title>
Chris@0 99 </head>
Chris@0 100 <body>
Chris@0 101 Redirecting to <a href="%1$s">%1$s</a>.
Chris@0 102 </body>
Chris@0 103 </html>', htmlspecialchars($url, ENT_QUOTES, 'UTF-8')));
Chris@0 104
Chris@0 105 $this->headers->set('Location', $url);
Chris@0 106
Chris@0 107 return $this;
Chris@0 108 }
Chris@0 109 }