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>
38 private $parameters = array();
43 * @param string $baseUrl The base URL
44 * @param string $method The HTTP method
45 * @param string $host The HTTP host name
46 * @param string $scheme The HTTP scheme
47 * @param int $httpPort The HTTP port
48 * @param int $httpsPort The HTTPS port
49 * @param string $path The path
50 * @param string $queryString The query string
52 public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
54 $this->setBaseUrl($baseUrl);
55 $this->setMethod($method);
56 $this->setHost($host);
57 $this->setScheme($scheme);
58 $this->setHttpPort($httpPort);
59 $this->setHttpsPort($httpsPort);
60 $this->setPathInfo($path);
61 $this->setQueryString($queryString);
65 * Updates the RequestContext information based on a HttpFoundation Request.
67 * @param Request $request A Request instance
71 public function fromRequest(Request $request)
73 $this->setBaseUrl($request->getBaseUrl());
74 $this->setPathInfo($request->getPathInfo());
75 $this->setMethod($request->getMethod());
76 $this->setHost($request->getHost());
77 $this->setScheme($request->getScheme());
78 $this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
79 $this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
80 $this->setQueryString($request->server->get('QUERY_STRING', ''));
88 * @return string The base URL
90 public function getBaseUrl()
92 return $this->baseUrl;
98 * @param string $baseUrl The base URL
102 public function setBaseUrl($baseUrl)
104 $this->baseUrl = $baseUrl;
110 * Gets the path info.
112 * @return string The path info
114 public function getPathInfo()
116 return $this->pathInfo;
120 * Sets the path info.
122 * @param string $pathInfo The path info
126 public function setPathInfo($pathInfo)
128 $this->pathInfo = $pathInfo;
134 * Gets the HTTP method.
136 * The method is always an uppercased string.
138 * @return string The HTTP method
140 public function getMethod()
142 return $this->method;
146 * Sets the HTTP method.
148 * @param string $method The HTTP method
152 public function setMethod($method)
154 $this->method = strtoupper($method);
160 * Gets the HTTP host.
162 * The host is always lowercased because it must be treated case-insensitive.
164 * @return string The HTTP host
166 public function getHost()
172 * Sets the HTTP host.
174 * @param string $host The HTTP host
178 public function setHost($host)
180 $this->host = strtolower($host);
186 * Gets the HTTP scheme.
188 * @return string The HTTP scheme
190 public function getScheme()
192 return $this->scheme;
196 * Sets the HTTP scheme.
198 * @param string $scheme The HTTP scheme
202 public function setScheme($scheme)
204 $this->scheme = strtolower($scheme);
210 * Gets the HTTP port.
212 * @return int The HTTP port
214 public function getHttpPort()
216 return $this->httpPort;
220 * Sets the HTTP port.
222 * @param int $httpPort The HTTP port
226 public function setHttpPort($httpPort)
228 $this->httpPort = (int) $httpPort;
234 * Gets the HTTPS port.
236 * @return int The HTTPS port
238 public function getHttpsPort()
240 return $this->httpsPort;
244 * Sets the HTTPS port.
246 * @param int $httpsPort The HTTPS port
250 public function setHttpsPort($httpsPort)
252 $this->httpsPort = (int) $httpsPort;
258 * Gets the query string.
260 * @return string The query string without the "?"
262 public function getQueryString()
264 return $this->queryString;
268 * Sets the query string.
270 * @param string $queryString The query string (after "?")
274 public function setQueryString($queryString)
276 // string cast to be fault-tolerant, accepting null
277 $this->queryString = (string) $queryString;
283 * Returns the parameters.
285 * @return array The parameters
287 public function getParameters()
289 return $this->parameters;
293 * Sets the parameters.
295 * @param array $parameters The parameters
299 public function setParameters(array $parameters)
301 $this->parameters = $parameters;
307 * Gets a parameter value.
309 * @param string $name A parameter name
311 * @return mixed The parameter value or null if nonexistent
313 public function getParameter($name)
315 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
319 * Checks if a parameter value is set for the given parameter.
321 * @param string $name A parameter name
323 * @return bool True if the parameter value is set, false otherwise
325 public function hasParameter($name)
327 return array_key_exists($name, $this->parameters);
331 * Sets a parameter value.
333 * @param string $name A parameter name
334 * @param mixed $parameter The parameter value
338 public function setParameter($name, $parameter)
340 $this->parameters[$name] = $parameter;