Router

class Router
property $default_format

Default regexp for matching variables.

Default:'[A-Za-z0-9\-_\.]+'
property $variable_formats

Regexps for matching specific variables.

$variable_formats = [
  'foo' => '\d+',
];

will only match digits when adding a route /:foo/

Default:[]
loadRoutes($filename)

Load routes from file.

formatRoutes($verbose = false)

Describe available routes in human-readable form.

Returns:String with description.
printRoutes($verbose = false)

Print available routes in human-readable form.

match($url, $method = false)

Match a request against routes.

Parameters:
  • $url (string) – Request URL.
  • $method (string|false) – Request method or false to read from $_SERVER['REQUEST_METHOD'].
Returns:

Matching route or null.

Return type:

RouterMatch

clear()

Removes all routes. Mostly useful for tests.

addRoute($pattern, $method, $options)

Add a new route.

Basic format is /path/to/match which literally matches the url. Variables can be assigned using /foo/:bar where the second part is a variable called bar. By default variables accept almost anything but the regular expression can be tuned using the option bar_format (see below). Patterns may also contain regular expressions.

Parameters:
  • $pattern (string) – Route pattern to match, see format description above.
  • $method (string) – Which HTTP method to match.
  • $options (array) –
    Array of options:
    • to Target controller/action. [controller][#action]
    • as Name of this route.
    • var_format Variable format. var should be replaced with the actual variable name, like ‘bar_format’. The value is the RE for matching, e.g. [0-9]+ for a required number.
Returns:

Generated path function.

context()

Create a new context for adding routes directly to root scope.