Explorar el Código

[HttpKernel] added Request::getQueryString()

Fabien Potencier hace 15 años
padre
commit
653620406b
Se han modificado 1 ficheros con 35 adiciones y 19 borrados
  1. 35 19
      src/Symfony/Components/HttpKernel/Request.php

+ 35 - 19
src/Symfony/Components/HttpKernel/Request.php

@@ -271,34 +271,50 @@ class Request
     /**
      * Generates a normalized URI for the Request.
      *
-     * It builds a normalized query string, where keys/value pairs are alphabetized
-     * and have consistent escaping.
-     *
      * @return string A normalized URI for the Request
+     *
+     * @see getQueryString()
      */
     public function getUri()
     {
-        $parts = $this->getScheme().'://'.$this->getHost().':'.$this->getPort().$this->getScriptName().$this->getPathInfo();
+        $qs = $this->getQueryString();
+        if (null !== $qs)
+        {
+            $qs = '?'.$qs;
+        }
 
-        if ($qs = $this->server->get('QUERY_STRING')) {
-            $parts = array();
-            foreach (explode('&', $qs) as $segment) {
-                $tmp = explode('=', urldecode($segment), 2);
-                $parts[urlencode($tmp[0])] = urlencode($tmp[1]);
-            }
-            ksort($parts);
+        return $this->getScheme().'://'.$this->getHost().':'.$this->getPort().$this->getScriptName().$this->getPathInfo().$qs;
+    }
 
-            $elements = array();
-            foreach ($parts as $key => $value) {
-                $elements[] = "$key=$value";
-            }
+    /**
+     * Generates the normalized query string for the Request.
+     *
+     * It builds a normalized query string, where keys/value pairs are alphabetized
+     * and have consistent escaping.
+     *
+     * @return string A normalized query string for the Request
+     */
+    public function getQueryString()
+    {
+        if (!$qs = $this->server->get('QUERY_STRING')) {
+            return null;
+        }
 
-            if (count($elements)) {
-                $parts .= '?'.implode('&', $elements);
-            }
+        $parts = array();
+        foreach (explode('&', $qs) as $segment) {
+            $tmp = explode('=', urldecode($segment), 2);
+            $parts[urlencode($tmp[0])] = urlencode($tmp[1]);
         }
+        ksort($parts);
 
-        return $parts;
+        $elements = array();
+        foreach ($parts as $key => $value) {
+            $elements[] = "$key=$value";
+        }
+
+        if (count($elements)) {
+            return implode('&', $elements);
+        }
     }
 
     public function isSecure()