Browse Source

added Response::setRedirect()

Fabien Potencier 14 years ago
parent
commit
f61bb19548

+ 1 - 5
src/Symfony/Bundle/FrameworkBundle/Controller.php

@@ -106,11 +106,7 @@ class Controller
      */
      */
     public function redirect($url, $status = 302)
     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);
     }
     }
 
 
     /**
     /**

+ 20 - 0
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('<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.
      * Returns true if the response includes a Vary header.
      *
      *