class Route implements Serializable

A Route describes a route and its parameters.

Methods

__construct(string $path, array $defaults = array(), array $requirements = array(), array $options = array(), string|null $host = '', string|string[] $schemes = array(), string|string[] $methods = array(), string|null $condition = '')

Constructor.

serialize()

{@inheritdoc}

unserialize($serialized)

{@inheritdoc}

string
getPath()

Returns the pattern for the path.

$this
setPath(string $pattern)

Sets the pattern for the path.

string
getHost()

Returns the pattern for the host.

$this
setHost(string $pattern)

Sets the pattern for the host.

string[]
getSchemes()

Returns the lowercased schemes this route is restricted to.

$this
setSchemes(string|string[] $schemes)

Sets the schemes (e.g. 'https') this route is restricted to.

bool
hasScheme(string $scheme)

Checks if a scheme requirement has been set.

string[]
getMethods()

Returns the uppercased HTTP methods this route is restricted to.

$this
setMethods(string|string[] $methods)

Sets the HTTP methods (e.g. 'POST') this route is restricted to.

array
getOptions()

Returns the options.

$this
setOptions(array $options)

Sets the options.

$this
addOptions(array $options)

Adds options.

$this
setOption(string $name, mixed $value)

Sets an option value.

mixed
getOption(string $name)

Get an option value.

bool
hasOption(string $name)

Checks if an option has been set.

array
getDefaults()

Returns the defaults.

$this
setDefaults(array $defaults)

Sets the defaults.

$this
addDefaults(array $defaults)

Adds defaults.

mixed
getDefault(string $name)

Gets a default value.

bool
hasDefault(string $name)

Checks if a default value is set for the given variable.

$this
setDefault(string $name, mixed $default)

Sets a default value.

array
getRequirements()

Returns the requirements.

$this
setRequirements(array $requirements)

Sets the requirements.

$this
addRequirements(array $requirements)

Adds requirements.

string|null
getRequirement(string $key)

Returns the requirement for the given key.

bool
hasRequirement(string $key)

Checks if a requirement is set for the given key.

$this
setRequirement(string $key, string $regex)

Sets a requirement for the given key.

string
getCondition()

Returns the condition.

$this
setCondition(string $condition)

Sets the condition.

compile()

Compiles the route.

Details

__construct(string $path, array $defaults = array(), array $requirements = array(), array $options = array(), string|null $host = '', string|string[] $schemes = array(), string|string[] $methods = array(), string|null $condition = '')

Constructor.

Available options:

  • compiler_class: A class name able to compile this route instance (RouteCompiler by default)
  • utf8: Whether UTF-8 matching is enforced ot not

Parameters

string $path The path pattern to match
array $defaults An array of default parameter values
array $requirements An array of requirements for parameters (regexes)
array $options An array of options
string|null $host The host pattern to match
string|string[] $schemes A required URI scheme or an array of restricted schemes
string|string[] $methods A required HTTP method or an array of restricted methods
string|null $condition A condition that should evaluate to true for the route to match

serialize()

{@inheritdoc}

unserialize($serialized)

{@inheritdoc}

Parameters

$serialized

string getPath()

Returns the pattern for the path.

Return Value

string The path pattern

$this setPath(string $pattern)

Sets the pattern for the path.

This method implements a fluent interface.

Parameters

string $pattern The path pattern

Return Value

$this

string getHost()

Returns the pattern for the host.

Return Value

string The host pattern

$this setHost(string $pattern)

Sets the pattern for the host.

This method implements a fluent interface.

Parameters

string $pattern The host pattern

Return Value

$this

string[] getSchemes()

Returns the lowercased schemes this route is restricted to.

So an empty array means that any scheme is allowed.

Return Value

string[] The schemes

$this setSchemes(string|string[] $schemes)

Sets the schemes (e.g. 'https') this route is restricted to.

So an empty array means that any scheme is allowed.

This method implements a fluent interface.

Parameters

string|string[] $schemes The scheme or an array of schemes

Return Value

$this

bool hasScheme(string $scheme)

Checks if a scheme requirement has been set.

Parameters

string $scheme

Return Value

bool true if the scheme requirement exists, otherwise false

string[] getMethods()

Returns the uppercased HTTP methods this route is restricted to.

So an empty array means that any method is allowed.

Return Value

string[] The methods

$this setMethods(string|string[] $methods)

Sets the HTTP methods (e.g. 'POST') this route is restricted to.

So an empty array means that any method is allowed.

This method implements a fluent interface.

Parameters

string|string[] $methods The method or an array of methods

Return Value

$this

array getOptions()

Returns the options.

Return Value

array The options

$this setOptions(array $options)

Sets the options.

This method implements a fluent interface.

Parameters

array $options The options

Return Value

$this

$this addOptions(array $options)

Adds options.

This method implements a fluent interface.

Parameters

array $options The options

Return Value

$this

$this setOption(string $name, mixed $value)

Sets an option value.

This method implements a fluent interface.

Parameters

string $name An option name
mixed $value The option value

Return Value

$this

mixed getOption(string $name)

Get an option value.

Parameters

string $name An option name

Return Value

mixed The option value or null when not given

bool hasOption(string $name)

Checks if an option has been set.

Parameters

string $name An option name

Return Value

bool true if the option is set, false otherwise

array getDefaults()

Returns the defaults.

Return Value

array The defaults

$this setDefaults(array $defaults)

Sets the defaults.

This method implements a fluent interface.

Parameters

array $defaults The defaults

Return Value

$this

$this addDefaults(array $defaults)

Adds defaults.

This method implements a fluent interface.

Parameters

array $defaults The defaults

Return Value

$this

mixed getDefault(string $name)

Gets a default value.

Parameters

string $name A variable name

Return Value

mixed The default value or null when not given

bool hasDefault(string $name)

Checks if a default value is set for the given variable.

Parameters

string $name A variable name

Return Value

bool true if the default value is set, false otherwise

$this setDefault(string $name, mixed $default)

Sets a default value.

Parameters

string $name A variable name
mixed $default The default value

Return Value

$this

array getRequirements()

Returns the requirements.

Return Value

array The requirements

$this setRequirements(array $requirements)

Sets the requirements.

This method implements a fluent interface.

Parameters

array $requirements The requirements

Return Value

$this

$this addRequirements(array $requirements)

Adds requirements.

This method implements a fluent interface.

Parameters

array $requirements The requirements

Return Value

$this

string|null getRequirement(string $key)

Returns the requirement for the given key.

Parameters

string $key The key

Return Value

string|null The regex or null when not given

bool hasRequirement(string $key)

Checks if a requirement is set for the given key.

Parameters

string $key A variable name

Return Value

bool true if a requirement is specified, false otherwise

$this setRequirement(string $key, string $regex)

Sets a requirement for the given key.

Parameters

string $key The key
string $regex The regex

Return Value

$this

string getCondition()

Returns the condition.

Return Value

string The condition

$this setCondition(string $condition)

Sets the condition.

This method implements a fluent interface.

Parameters

string $condition The condition

Return Value

$this

CompiledRoute compile()

Compiles the route.

Return Value

CompiledRoute A CompiledRoute instance

Exceptions

LogicException If the Route cannot be compiled because the path or host pattern is invalid

See also