added Response::setRedirect()

This commit is contained in:
Fabien Potencier 2010-08-14 19:51:11 +02:00
parent ef0347c1b9
commit f61bb19548
2 changed files with 21 additions and 5 deletions

View File

@ -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);
}
/**

View File

@ -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('<html><head><meta http-equiv="refresh" content="1;url=%s"/></head></html>', htmlspecialchars($url, ENT_QUOTES)));
}
/**
* Returns true if the response includes a Vary header.
*