Router
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
Finds a page by its URL. Returns the page object and sets the $parameters property.
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.
Autoloads the URL map only allowing a single execution.
Autoloads the URL map only allowing a single execution.
Loads the URL map - a list of page file names and corresponding URL patterns.
Clears the router cache.
Sets the current routing parameters.
Returns the current routing parameters.
Returns the last URL to be looked up.
Returns a routing parameter.
Returns the caching URL key depending on the theme.
Returns the cache key name for the URL list.
Returns the cache key name for the URL list.
Tries to load a page file name corresponding to a specified URL from the cache.
Details
at line 66
__construct(Theme $theme)
No description
at line 79
Page|mixed
findByUrl(string $url)
Finds a page by its URL. Returns the page object and sets the $parameters property.
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.
at line 167
protected Router
getRouterObject()
Autoloads the URL map only allowing a single execution.
at line 189
protected array
getUrlMap()
Autoloads the URL map only allowing a single execution.
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.
at line 241
clearCache()
Clears the router cache.
at line 254
void
setParameters(array $parameters)
Sets the current routing parameters.
at line 263
array
getParameters()
Returns the current routing parameters.
at line 272
string
getUrl()
Returns the last URL to be looked up.
at line 285
array
getParameter($name, $default = null)
Returns a routing parameter.
at line 301
protected string
getCacheKey(string $keyName)
Returns the caching URL key depending on the theme.
at line 310
protected string
getUrlListCacheKey()
Returns the cache key name for the URL list.
at line 319
protected string
getUrlMapCacheKey()
Returns the cache key name for the URL list.
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.