4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing;
14 use Symfony\Component\HttpFoundation\Request;
17 * Holds information about the current request.
19 * This class implements a fluent interface.
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Tobias Schultze <http://tobion.de>
34 private $parameters = array();
37 * @param string $baseUrl The base URL
38 * @param string $method The HTTP method
39 * @param string $host The HTTP host name
40 * @param string $scheme The HTTP scheme
41 * @param int $httpPort The HTTP port
42 * @param int $httpsPort The HTTPS port
43 * @param string $path The path
44 * @param string $queryString The query string
46 public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
48 $this->setBaseUrl($baseUrl);
49 $this->setMethod($method);
50 $this->setHost($host);
51 $this->setScheme($scheme);
52 $this->setHttpPort($httpPort);
53 $this->setHttpsPort($httpsPort);
54 $this->setPathInfo($path);
55 $this->setQueryString($queryString);
59 * Updates the RequestContext information based on a HttpFoundation Request.
63 public function fromRequest(Request $request)
65 $this->setBaseUrl($request->getBaseUrl());
66 $this->setPathInfo($request->getPathInfo());
67 $this->setMethod($request->getMethod());
68 $this->setHost($request->getHost());
69 $this->setScheme($request->getScheme());
70 $this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
71 $this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
72 $this->setQueryString($request->server->get('QUERY_STRING', ''));
80 * @return string The base URL
82 public function getBaseUrl()
84 return $this->baseUrl;
90 * @param string $baseUrl The base URL
94 public function setBaseUrl($baseUrl)
96 $this->baseUrl = $baseUrl;
102 * Gets the path info.
104 * @return string The path info
106 public function getPathInfo()
108 return $this->pathInfo;
112 * Sets the path info.
114 * @param string $pathInfo The path info
118 public function setPathInfo($pathInfo)
120 $this->pathInfo = $pathInfo;
126 * Gets the HTTP method.
128 * The method is always an uppercased string.
130 * @return string The HTTP method
132 public function getMethod()
134 return $this->method;
138 * Sets the HTTP method.
140 * @param string $method The HTTP method
144 public function setMethod($method)
146 $this->method = strtoupper($method);
152 * Gets the HTTP host.
154 * The host is always lowercased because it must be treated case-insensitive.
156 * @return string The HTTP host
158 public function getHost()
164 * Sets the HTTP host.
166 * @param string $host The HTTP host
170 public function setHost($host)
172 $this->host = strtolower($host);
178 * Gets the HTTP scheme.
180 * @return string The HTTP scheme
182 public function getScheme()
184 return $this->scheme;
188 * Sets the HTTP scheme.
190 * @param string $scheme The HTTP scheme
194 public function setScheme($scheme)
196 $this->scheme = strtolower($scheme);
202 * Gets the HTTP port.
204 * @return int The HTTP port
206 public function getHttpPort()
208 return $this->httpPort;
212 * Sets the HTTP port.
214 * @param int $httpPort The HTTP port
218 public function setHttpPort($httpPort)
220 $this->httpPort = (int) $httpPort;
226 * Gets the HTTPS port.
228 * @return int The HTTPS port
230 public function getHttpsPort()
232 return $this->httpsPort;
236 * Sets the HTTPS port.
238 * @param int $httpsPort The HTTPS port
242 public function setHttpsPort($httpsPort)
244 $this->httpsPort = (int) $httpsPort;
250 * Gets the query string.
252 * @return string The query string without the "?"
254 public function getQueryString()
256 return $this->queryString;
260 * Sets the query string.
262 * @param string $queryString The query string (after "?")
266 public function setQueryString($queryString)
268 // string cast to be fault-tolerant, accepting null
269 $this->queryString = (string) $queryString;
275 * Returns the parameters.
277 * @return array The parameters
279 public function getParameters()
281 return $this->parameters;
285 * Sets the parameters.
287 * @param array $parameters The parameters
291 public function setParameters(array $parameters)
293 $this->parameters = $parameters;
299 * Gets a parameter value.
301 * @param string $name A parameter name
303 * @return mixed The parameter value or null if nonexistent
305 public function getParameter($name)
307 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
311 * Checks if a parameter value is set for the given parameter.
313 * @param string $name A parameter name
315 * @return bool True if the parameter value is set, false otherwise
317 public function hasParameter($name)
319 return array_key_exists($name, $this->parameters);
323 * Sets a parameter value.
325 * @param string $name A parameter name
326 * @param mixed $parameter The parameter value
330 public function setParameter($name, $parameter)
332 $this->parameters[$name] = $parameter;