diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller.php index abdd06a940..cae6aed465 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller.php @@ -106,11 +106,7 @@ class Controller */ public function redirect($url, $status = 302) { - $response = $this->container->get('response'); - $response->setStatusCode($status); - $response->headers->set('Location', $url); - - return $response; + return $this->container->get('response')->setRedirect($url, $status); } /** diff --git a/src/Symfony/Components/HttpFoundation/Response.php b/src/Symfony/Components/HttpFoundation/Response.php index a8f263a293..11b0648d81 100644 --- a/src/Symfony/Components/HttpFoundation/Response.php +++ b/src/Symfony/Components/HttpFoundation/Response.php @@ -535,6 +535,26 @@ class Response } } + /** + * Modifies the response so that it conforms to the rules defined for a redirect status code. + * + * @see http://tools.ietf.org/html/rfc2616#section-10.3.5 + */ + public function setRedirect($url, $status = 302) + { + if (empty($url)) { + throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); + } + + $this->setStatusCode($status); + if (!$this->isRedirect()) { + throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status)); + } + + $this->headers->set('Location', $url); + $this->setContent(sprintf('', htmlspecialchars($url, ENT_QUOTES))); + } + /** * Returns true if the response includes a Vary header. *