class AltoRouter

Properties

protected array $routes
protected array $namedRoutes
protected string $basePath
protected array $matchTypes

Methods

__construct(array $routes = array(), string $basePath = '', array $matchTypes = array())

Create router in one call from config.

array
getRoutes()

Retrieves all routes.

void
addRoutes(array $routes)

Add multiple routes at once from array in the following format:

setBasePath($basePath)

Set the base path.

addMatchTypes(array $matchTypes)

Add named match types. It uses array_merge so keys can be overwritten.

map(string $method, string $route, mixed $target, string $name = null)

Map a route to a target

string
generate(string $routeName, array $params = array())

Reversed routing

array|bool
match(string $requestUrl = null, string $requestMethod = null)

Match a given Request Url against stored routes

compileRoute($route)

Compile the regex for a given route (EXPENSIVE)

Details

at line 39
__construct(array $routes = array(), string $basePath = '', array $matchTypes = array())

Create router in one call from config.

Parameters

array $routes
string $basePath
array $matchTypes

at line 50
array getRoutes()

Retrieves all routes.

Useful if you want to process or display routes.

Return Value

array

All routes.

at line 66
void addRoutes(array $routes)

Add multiple routes at once from array in the following format:

$routes = array( array($method, $route, $target, $name) );

Parameters

array $routes

Return Value

void

Exceptions

Exception

at line 79
setBasePath($basePath)

Set the base path.

Useful if you are running your application from a subdirectory.

Parameters

$basePath

at line 88
addMatchTypes(array $matchTypes)

Add named match types. It uses array_merge so keys can be overwritten.

Parameters

array $matchTypes

The key is the name and the value is the regex.

at line 101
map(string $method, string $route, mixed $target, string $name = null)

Map a route to a target

Parameters

string $method

One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE)

string $route

The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]

mixed $target

The target where this route should point to. Can be anything.

string $name

Optional name of this route. Supply if you want to reverse route this url in your application.

Exceptions

Exception

at line 127
string generate(string $routeName, array $params = array())

Reversed routing

Generate the URL for a named route. Replace regexes with supplied parameters

Parameters

string $routeName

The name of the route.

array $params

Return Value

string

The URL of the route with named parameters in place.

Exceptions

Exception

at line 172
array|bool match(string $requestUrl = null, string $requestMethod = null)

Match a given Request Url against stored routes

Parameters

string $requestUrl
string $requestMethod

Return Value

array|bool

Array with route information on success, false on failure (no match).

at line 243
protected compileRoute($route)

Compile the regex for a given route (EXPENSIVE)

Parameters

$route