RedirectResponse.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * @deprecated since Symfony 5.1, use __construct() instead.
  50. */
  51. public static function create($url = '', int $status = 302, array $headers = [])
  52. {
  53. trigger_deprecation('symfony/http-foundation', '5.1', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, static::class);
  54. return new static($url, $status, $headers);
  55. }
  56. /**
  57. * Returns the target URL.
  58. *
  59. * @return string target URL
  60. */
  61. public function getTargetUrl()
  62. {
  63. return $this->targetUrl;
  64. }
  65. /**
  66. * Sets the redirect target of this response.
  67. *
  68. * @return $this
  69. *
  70. * @throws \InvalidArgumentException
  71. */
  72. public function setTargetUrl(string $url)
  73. {
  74. if ('' === $url) {
  75. throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
  76. }
  77. $this->targetUrl = $url;
  78. $this->setContent(
  79. sprintf('<!DOCTYPE html>
  80. <html>
  81. <head>
  82. <meta charset="UTF-8" />
  83. <meta http-equiv="refresh" content="0;url=\'%1$s\'" />
  84. <title>Redirecting to %1$s</title>
  85. </head>
  86. <body>
  87. Redirecting to <a href="%1$s">%1$s</a>.
  88. </body>
  89. </html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8')));
  90. $this->headers->set('Location', $url);
  91. return $this;
  92. }
  93. }