Contents

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.

protected $aliasRouterObj

October\Rain\Router\Router Router object with default URLs that have translated overrides.


Public Methods

public __construct()

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

__construct the router instance.

public build()

public build(System\Models\SiteDefinition|null $site = null): RainRouter 

build builds a rain router based on the theme, with translated URLs for a site.

public buildAlias()

public buildAlias(
    System\Models\SiteDefinition|null $site = null
): RainRouter 

buildAlias builds a rain router of default URLs that are translated for a site.

public clearCache()

public clearCache(): void

clearCache invalidates the router cache for every locale by bumping the cache generation.

public findAliasRedirect()

public findAliasRedirect(string $url): string|null 

findAliasRedirect matches a URL against default page URLs that have been replaced by a translated URL, returning the translated path to redirect to.

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 buildAliasRouterObject()

protected buildAliasRouterObject($site = null): void

buildAliasRouterObject builds a map of default URLs for pages with a translated URL.

protected buildCachedAliasRouterObject()

protected buildCachedAliasRouterObject(): void

protected buildCachedRouterObject()

protected buildCachedRouterObject(): void

protected buildRouterObject()

protected buildRouterObject($site = null): void

buildRouterObject builds the URL map, substituting translated URLs for a site.

protected getAliasRouteCache()

protected getAliasRouteCache(): void

getAliasRouteCache returns the cached alias map, or null when not cached.

protected getAliasRouteCacheKey()

protected getAliasRouteCacheKey(): string 

getAliasRouteCacheKey returns the cache key name for the alias URL list.

protected getAliasRouterObject()

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

getAliasRouterObject autoloads the alias URL map only allowing a single execution.

protected getCacheGeneration()

protected getCacheGeneration(): int 

getCacheGeneration returns the current router cache generation number.

protected getCacheKey()

protected getCacheKey(string $keyName): string 

getCacheKey returns the caching URL key depending on the theme.

protected getManifestAliasRouteCache()

protected getManifestAliasRouteCache(): array 

getManifestAliasRouteCache returns the cached alias route map from the theme

protected getManifestArray()

protected getManifestArray(): array 

getManifestArray returns the theme cache manifest contents.

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 putAliasRouteCache()

protected putAliasRouteCache($urlMap): void

protected putMapRouteCache()

protected putMapRouteCache($urlMap): void