class Router

Router Class The router parses page URL patterns and finds pages by URLs.

The page URL format is explained below.

/pages/:page_id

Name of parameters should be compatible with PHP variable names. To make a parameter optional add the question mark after its name:

/pages/:page_id?

By default parameters in the middle of the URL are required, for example:

/pages/:page_id?/comments - although the :post_id parameter is marked as optional,
it will be processed as required.

Optional parameters can have default values which are used as fallback values in case if the real parameter value is not presented in the URL. Default values cannot contain the pipe symbols and question marks. Specify the default value after the question mark:

/pages/category/:page_id?10 - The page_id parameter would be 10 for this URL: /pages/category

You can also add regular expression validation to parameters. To add a validation expression add the pipe symbol after the parameter name (or the question mark) and specify the expression. The forward slash symbol is not allowed in the expressions. Examples:

/pages/:page_id|^[0-9]+$/comments - this will match /pages/10/comments
/pages/:page_id|^[0-9]+$ - this will match /pages/3
/pages/:page_name?|^[a-z0-9\-]+$ - this will match /pages/my-page

Based on october\cms\Router

Properties

protected Theme $theme
protected string $url
protected array $parameters
protected array $urlMap
protected $routerObj

Router object with routes preloaded.

Methods

__construct(Theme $theme)

No description

Page|mixed
findByUrl(string $url)

Finds a page by its URL. Returns the page object and sets the $parameters property.

string
findByFile(string $fileName, array $parameters = [])

Finds a URL by it's page. Returns the URL route for linking to the page and uses the supplied parameters in it's address.

getRouterObject()

Autoloads the URL map only allowing a single execution.

array
getUrlMap()

Autoloads the URL map only allowing a single execution.

bool
loadUrlMap()

Loads the URL map - a list of page file names and corresponding URL patterns.

clearCache()

Clears the router cache.

void
setParameters(array $parameters)

Sets the current routing parameters.

array
getParameters()

Returns the current routing parameters.

string
getUrl()

Returns the last URL to be looked up.

array
getParameter($name, $default = null)

Returns a routing parameter.

string
getCacheKey(string $keyName)

Returns the caching URL key depending on the theme.

string
getUrlListCacheKey()

Returns the cache key name for the URL list.

string
getUrlMapCacheKey()

Returns the cache key name for the URL list.

mixed
getCachedUrlFileName(string $url, array $urlList)

Tries to load a page file name corresponding to a specified URL from the cache.

Details

at line 66
__construct(Theme $theme)

No description

Parameters

Theme $theme

at line 79
Page|mixed findByUrl(string $url)

Finds a page by its URL. Returns the page object and sets the $parameters property.

Parameters

string $url

The requested URL string.

Return Value

Page|mixed

Returns page object or null if the page cannot be found.

at line 154
string findByFile(string $fileName, array $parameters = [])

Finds a URL by it's page. Returns the URL route for linking to the page and uses the supplied parameters in it's address.

Parameters

string $fileName

Page file name.

array $parameters

Route parameters to consider in the URL.

Return Value

string

A built URL matching the page route.

at line 167
protected Router getRouterObject()

Autoloads the URL map only allowing a single execution.

Return Value

Router

at line 189
protected array getUrlMap()

Autoloads the URL map only allowing a single execution.

Return Value

array

Returns the URL map.

at line 205
protected bool loadUrlMap()

Loads the URL map - a list of page file names and corresponding URL patterns.

The URL map can is cached. The clearUrlMap() method resets the cache. By default the map is updated every time when a page is saved in the back-end, or when the interval defined with the system.urlMapCacheTtl expires.

Return Value

bool

Returns true if the URL map was loaded from the cache. Otherwise returns false.

at line 241
clearCache()

Clears the router cache.

at line 254
void setParameters(array $parameters)

Sets the current routing parameters.

Parameters

array $parameters

Return Value

void

at line 263
array getParameters()

Returns the current routing parameters.

Return Value

array

at line 272
string getUrl()

Returns the last URL to be looked up.

Return Value

string

at line 285
array getParameter($name, $default = null)

Returns a routing parameter.

Parameters

$name
$default

Return Value

array

at line 301
protected string getCacheKey(string $keyName)

Returns the caching URL key depending on the theme.

Parameters

string $keyName

Specifies the base key name.

Return Value

string

Returns the theme-specific key name.

at line 310
protected string getUrlListCacheKey()

Returns the cache key name for the URL list.

Return Value

string

at line 319
protected string getUrlMapCacheKey()

Returns the cache key name for the URL list.

Return Value

string

at line 332
protected mixed getCachedUrlFileName(string $url, array $urlList)

Tries to load a page file name corresponding to a specified URL from the cache.

Parameters

string $url

Specifies the requested URL.

array $urlList

The URL list loaded from the cache

Return Value

mixed

Returns the page file name if the URL exists in the cache. Otherwise returns null.