RedirectResponse.php 2.8 KB

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