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;
15 * A Route describes a route and its parameters.
17 * @author Fabien Potencier <fabien@symfony.com>
18 * @author Tobias Schultze <http://tobion.de>
20 class Route implements \Serializable
24 private $schemes = array();
25 private $methods = array();
26 private $defaults = array();
27 private $requirements = array();
28 private $options = array();
29 private $condition = '';
32 * @var CompiledRoute|null
41 * * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
42 * * utf8: Whether UTF-8 matching is enforced ot not
44 * @param string $path The path pattern to match
45 * @param array $defaults An array of default parameter values
46 * @param array $requirements An array of requirements for parameters (regexes)
47 * @param array $options An array of options
48 * @param string $host The host pattern to match
49 * @param string|string[] $schemes A required URI scheme or an array of restricted schemes
50 * @param string|string[] $methods A required HTTP method or an array of restricted methods
51 * @param string $condition A condition that should evaluate to true for the route to match
53 public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', $schemes = array(), $methods = array(), $condition = '')
55 $this->setPath($path);
56 $this->setDefaults($defaults);
57 $this->setRequirements($requirements);
58 $this->setOptions($options);
59 $this->setHost($host);
60 $this->setSchemes($schemes);
61 $this->setMethods($methods);
62 $this->setCondition($condition);
68 public function serialize()
70 return serialize(array(
71 'path' => $this->path,
72 'host' => $this->host,
73 'defaults' => $this->defaults,
74 'requirements' => $this->requirements,
75 'options' => $this->options,
76 'schemes' => $this->schemes,
77 'methods' => $this->methods,
78 'condition' => $this->condition,
79 'compiled' => $this->compiled,
86 public function unserialize($serialized)
88 $data = unserialize($serialized);
89 $this->path = $data['path'];
90 $this->host = $data['host'];
91 $this->defaults = $data['defaults'];
92 $this->requirements = $data['requirements'];
93 $this->options = $data['options'];
94 $this->schemes = $data['schemes'];
95 $this->methods = $data['methods'];
97 if (isset($data['condition'])) {
98 $this->condition = $data['condition'];
100 if (isset($data['compiled'])) {
101 $this->compiled = $data['compiled'];
106 * Returns the pattern for the path.
108 * @return string The path pattern
110 public function getPath()
116 * Sets the pattern for the path.
118 * This method implements a fluent interface.
120 * @param string $pattern The path pattern
124 public function setPath($pattern)
126 // A pattern must start with a slash and must not have multiple slashes at the beginning because the
127 // generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
128 $this->path = '/'.ltrim(trim($pattern), '/');
129 $this->compiled = null;
135 * Returns the pattern for the host.
137 * @return string The host pattern
139 public function getHost()
145 * Sets the pattern for the host.
147 * This method implements a fluent interface.
149 * @param string $pattern The host pattern
153 public function setHost($pattern)
155 $this->host = (string) $pattern;
156 $this->compiled = null;
162 * Returns the lowercased schemes this route is restricted to.
163 * So an empty array means that any scheme is allowed.
165 * @return string[] The schemes
167 public function getSchemes()
169 return $this->schemes;
173 * Sets the schemes (e.g. 'https') this route is restricted to.
174 * So an empty array means that any scheme is allowed.
176 * This method implements a fluent interface.
178 * @param string|string[] $schemes The scheme or an array of schemes
182 public function setSchemes($schemes)
184 $this->schemes = array_map('strtolower', (array) $schemes);
185 $this->compiled = null;
191 * Checks if a scheme requirement has been set.
193 * @param string $scheme
195 * @return bool true if the scheme requirement exists, otherwise false
197 public function hasScheme($scheme)
199 return \in_array(strtolower($scheme), $this->schemes, true);
203 * Returns the uppercased HTTP methods this route is restricted to.
204 * So an empty array means that any method is allowed.
206 * @return string[] The methods
208 public function getMethods()
210 return $this->methods;
214 * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
215 * So an empty array means that any method is allowed.
217 * This method implements a fluent interface.
219 * @param string|string[] $methods The method or an array of methods
223 public function setMethods($methods)
225 $this->methods = array_map('strtoupper', (array) $methods);
226 $this->compiled = null;
232 * Returns the options.
234 * @return array The options
236 public function getOptions()
238 return $this->options;
244 * This method implements a fluent interface.
246 * @param array $options The options
250 public function setOptions(array $options)
252 $this->options = array(
253 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
256 return $this->addOptions($options);
262 * This method implements a fluent interface.
264 * @param array $options The options
268 public function addOptions(array $options)
270 foreach ($options as $name => $option) {
271 $this->options[$name] = $option;
273 $this->compiled = null;
279 * Sets an option value.
281 * This method implements a fluent interface.
283 * @param string $name An option name
284 * @param mixed $value The option value
288 public function setOption($name, $value)
290 $this->options[$name] = $value;
291 $this->compiled = null;
297 * Get an option value.
299 * @param string $name An option name
301 * @return mixed The option value or null when not given
303 public function getOption($name)
305 return isset($this->options[$name]) ? $this->options[$name] : null;
309 * Checks if an option has been set.
311 * @param string $name An option name
313 * @return bool true if the option is set, false otherwise
315 public function hasOption($name)
317 return array_key_exists($name, $this->options);
321 * Returns the defaults.
323 * @return array The defaults
325 public function getDefaults()
327 return $this->defaults;
333 * This method implements a fluent interface.
335 * @param array $defaults The defaults
339 public function setDefaults(array $defaults)
341 $this->defaults = array();
343 return $this->addDefaults($defaults);
349 * This method implements a fluent interface.
351 * @param array $defaults The defaults
355 public function addDefaults(array $defaults)
357 foreach ($defaults as $name => $default) {
358 $this->defaults[$name] = $default;
360 $this->compiled = null;
366 * Gets a default value.
368 * @param string $name A variable name
370 * @return mixed The default value or null when not given
372 public function getDefault($name)
374 return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
378 * Checks if a default value is set for the given variable.
380 * @param string $name A variable name
382 * @return bool true if the default value is set, false otherwise
384 public function hasDefault($name)
386 return array_key_exists($name, $this->defaults);
390 * Sets a default value.
392 * @param string $name A variable name
393 * @param mixed $default The default value
397 public function setDefault($name, $default)
399 $this->defaults[$name] = $default;
400 $this->compiled = null;
406 * Returns the requirements.
408 * @return array The requirements
410 public function getRequirements()
412 return $this->requirements;
416 * Sets the requirements.
418 * This method implements a fluent interface.
420 * @param array $requirements The requirements
424 public function setRequirements(array $requirements)
426 $this->requirements = array();
428 return $this->addRequirements($requirements);
434 * This method implements a fluent interface.
436 * @param array $requirements The requirements
440 public function addRequirements(array $requirements)
442 foreach ($requirements as $key => $regex) {
443 $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
445 $this->compiled = null;
451 * Returns the requirement for the given key.
453 * @param string $key The key
455 * @return string|null The regex or null when not given
457 public function getRequirement($key)
459 return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
463 * Checks if a requirement is set for the given key.
465 * @param string $key A variable name
467 * @return bool true if a requirement is specified, false otherwise
469 public function hasRequirement($key)
471 return array_key_exists($key, $this->requirements);
475 * Sets a requirement for the given key.
477 * @param string $key The key
478 * @param string $regex The regex
482 public function setRequirement($key, $regex)
484 $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
485 $this->compiled = null;
491 * Returns the condition.
493 * @return string The condition
495 public function getCondition()
497 return $this->condition;
501 * Sets the condition.
503 * This method implements a fluent interface.
505 * @param string $condition The condition
509 public function setCondition($condition)
511 $this->condition = (string) $condition;
512 $this->compiled = null;
518 * Compiles the route.
520 * @return CompiledRoute A CompiledRoute instance
522 * @throws \LogicException If the Route cannot be compiled because the
523 * path or host pattern is invalid
525 * @see RouteCompiler which is responsible for the compilation process
527 public function compile()
529 if (null !== $this->compiled) {
530 return $this->compiled;
533 $class = $this->getOption('compiler_class');
535 return $this->compiled = $class::compile($this);
538 private function sanitizeRequirement($key, $regex)
540 if (!\is_string($regex)) {
541 throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
544 if ('' !== $regex && '^' === $regex[0]) {
545 $regex = (string) substr($regex, 1); // returns false for a single character
548 if ('$' === substr($regex, -1)) {
549 $regex = substr($regex, 0, -1);
553 throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));