Cms\Classes\Router

Overview

Router parses page URL patterns and finds pages by URLs.

The page URL format is explained below.

/blog/post/:post_id

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

/blog/post/:post_id?

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

/blog/:post_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:

/blog/category/:category_id?10 - The category_id parameter would be 10 for this URL: /blog/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:

/blog/:post_id|^[0-9]+$/comments - this will match /blog/post/10/comments
/blog/:post_id|^[0-9]+$ - this will match /blog/post/3
/blog/:post_name?|^[a-z0-9\-]+$ - this will match /blog/my-blog-post

Protected Properties

protected Cms\Classes\Theme $theme

A reference to the CMS theme containing the object.

protected string $url

The last URL to be looked up using findByUrl().

protected array $parameters

A list of parameters names and values extracted from the URL pattern and URL string.

protected $routerObj

October\Rain\Router\Router Router object with routes preloaded.


Public Methods

public __construct()

public __construct(Cms\Classes\Theme $theme): void

__construct the router instance.

public build()

public build(): RainRouter 

build builds a rain router based on the theme

public clearCache()

public clearCache(): void

clearCache clears the router cache.

public findByFile()

public findByFile(
    string $fileName,
    array $parameters = []
): string|null 

findByFile finds a URL by its page. Returns the URL route for linking to the page and uses the supplied parameters in its address.

public findByUrl()

public findByUrl(string $url): Cms\Classes\Page 

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

public getParameter()

public getParameter($name, $default = null): array 

getParameter returns a routing parameter.

public getParameters()

public getParameters(): array 

getParameters returns the current routing parameters.

public getUrl()

public getUrl(): string 

getUrl returns the last URL to be looked up.

public setParameters()

public setParameters(array $parameters): array 

setParameters sets the current routing parameters.

Protected Methods

protected buildCachedRouterObject()

protected buildCachedRouterObject(): void

protected buildRouterObject()

protected buildRouterObject(): void

protected getCacheKey()

protected getCacheKey(string $keyName): string 

getCacheKey returns the caching URL key depending on the theme.

protected getManifestRouteCache()

protected getManifestRouteCache(): array 

getManifestRouteCache returns the cached route map from the theme

protected getMapRouteCache()

protected getMapRouteCache(): void

protected getMapRouteCacheKey()

protected getMapRouteCacheKey(): string 

getMapRouteCacheKey returns the cache key name for the URL list.

protected getRouterObject()

protected getRouterObject(): October\Rain\Router\Router 

getRouterObject autoloads the URL map only allowing a single execution.

protected getUrlRouteCache()

protected getUrlRouteCache(string $url, array &$urlList): mixed 

getUrlRouteCache tries to load a page file name corresponding to a specified URL from the cache. Working with the URL list loaded from the cache. Returns the page file name if the URL exists in the cache. Otherwise returns null.

protected getUrlRouteCacheKey()

protected getUrlRouteCacheKey(): string 

getUrlRouteCacheKey returns the cache key name for the URL list.

protected putMapRouteCache()

protected putMapRouteCache($urlMap): void

protected putUrlRouteCache()

protected putUrlRouteCache(string $url, array $urlList, $urlList): void

putUrlRouteCache stored in cache