Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation; Chris@0: Chris@0: /** Chris@0: * RedirectResponse represents an HTTP response doing a redirect. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class RedirectResponse extends Response Chris@0: { Chris@0: protected $targetUrl; Chris@0: Chris@0: /** Chris@0: * Creates a redirect response so that it conforms to the rules defined for a redirect status code. Chris@0: * Chris@0: * @param string $url The URL to redirect to. The URL should be a full URL, with schema etc., Chris@0: * but practically every browser redirects on paths only as well Chris@0: * @param int $status The status code (302 by default) Chris@0: * @param array $headers The headers (Location is always set to the given URL) Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: * Chris@0: * @see http://tools.ietf.org/html/rfc2616#section-10.3 Chris@0: */ Chris@17: public function __construct($url, $status = 302, $headers = []) Chris@0: { Chris@0: parent::__construct('', $status, $headers); Chris@0: Chris@0: $this->setTargetUrl($url); Chris@0: Chris@0: if (!$this->isRedirect()) { Chris@0: throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status)); Chris@0: } Chris@0: Chris@18: if (301 == $status && !\array_key_exists('cache-control', $headers)) { Chris@0: $this->headers->remove('cache-control'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * Factory method for chainability. Chris@14: * Chris@14: * @param string $url The url to redirect to Chris@14: * @param int $status The response status code Chris@14: * @param array $headers An array of response headers Chris@14: * Chris@14: * @return static Chris@0: */ Chris@17: public static function create($url = '', $status = 302, $headers = []) Chris@0: { Chris@0: return new static($url, $status, $headers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the target URL. Chris@0: * Chris@0: * @return string target URL Chris@0: */ Chris@0: public function getTargetUrl() Chris@0: { Chris@0: return $this->targetUrl; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the redirect target of this response. Chris@0: * Chris@0: * @param string $url The URL to redirect to Chris@0: * Chris@0: * @return $this Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: public function setTargetUrl($url) Chris@0: { Chris@0: if (empty($url)) { Chris@0: throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); Chris@0: } Chris@0: Chris@0: $this->targetUrl = $url; Chris@0: Chris@0: $this->setContent( Chris@0: sprintf(' Chris@0: Chris@0: Chris@0: Chris@12: Chris@0: Chris@0: Redirecting to %1$s Chris@0: Chris@0: Chris@0: Redirecting to %1$s. Chris@0: Chris@0: ', htmlspecialchars($url, ENT_QUOTES, 'UTF-8'))); Chris@0: Chris@0: $this->headers->set('Location', $url); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: }