UrlHelper
extends Helper
in package
UrlHelper class for generating URLs.
Table of Contents
Properties
- $_assetUrlClassName : string
- Asset URL engine class name
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default config for this class
- $_helperMap : array<string, array<string|int, mixed>>
- A helper lookup table used to lazy load helper objects.
- $_View : View
- The View instance this helper is attached to
- $helpers : array<string|int, mixed>
- List of helpers used by this helper
Methods
- __call() : mixed|void
- Provide non fatal errors on missing method calls.
- __construct() : mixed
- Default Constructor
- __debugInfo() : array<string, mixed>
- Returns an array that can be used to describe the internal state of this object.
- __get() : Helper|null|void
- Lazy loads helpers.
- addClass() : array<string, mixed>
- Adds the given class to the element options
- assetTimestamp() : string
- Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in Configure. If Asset.timestamp is true and debug is true, or Asset.timestamp === 'force' a timestamp will be added.
- assetUrl() : string
- Generates URL for given asset file.
- build() : string
- Returns a URL based on provided parameters.
- buildFromPath() : string
- Returns a URL from a route path string.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- css() : string
- Generates URL for given CSS file.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- getView() : View
- Get the view instance this helper is bound to.
- image() : string
- Generates URL for given image file.
- implementedEvents() : array<string, mixed>
- Event listeners.
- initialize() : void
- Check proper configuration
- script() : string
- Generates URL for given javascript file.
- setConfig() : $this
- Sets the config.
- webroot() : string
- Checks if a file exists when theme is used, if no file is found default location is returned
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _confirm() : string
- Returns a string to be used as onclick handler for confirm dialogs.
Properties
$_assetUrlClassName
Asset URL engine class name
protected
string
$_assetUrlClassName
Tags
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Default config for this class
protected
array<string, mixed>
$_defaultConfig
= ['assetUrlClassName' => \Cake\Routing\Asset::class]
$_helperMap
A helper lookup table used to lazy load helper objects.
protected
array<string, array<string|int, mixed>>
$_helperMap
= []
$_View
The View instance this helper is attached to
protected
View
$_View
$helpers
List of helpers used by this helper
protected
array<string|int, mixed>
$helpers
= []
Methods
__call()
Provide non fatal errors on missing method calls.
public
__call(string $method, array<string|int, mixed> $params) : mixed|void
Parameters
- $method : string
-
Method to invoke
- $params : array<string|int, mixed>
-
Array of params for the method.
Return values
mixed|void__construct()
Default Constructor
public
__construct(View $view[, array<string, mixed> $config = [] ]) : mixed
Parameters
- $view : View
-
The View this helper is being attached to.
- $config : array<string, mixed> = []
-
Configuration settings for the helper.
__debugInfo()
Returns an array that can be used to describe the internal state of this object.
public
__debugInfo() : array<string, mixed>
Return values
array<string, mixed>__get()
Lazy loads helpers.
public
__get(string $name) : Helper|null|void
Parameters
- $name : string
-
Name of the property being accessed.
Return values
Helper|null|void —Helper instance if helper with provided name exists
addClass()
Adds the given class to the element options
public
addClass(array<string, mixed> $options, string $class[, string $key = 'class' ]) : array<string, mixed>
Parameters
- $options : array<string, mixed>
-
Array options/attributes to add a class to
- $class : string
-
The class name being added.
- $key : string = 'class'
-
the key to use for class. Defaults to
'class'
.
Return values
array<string, mixed> —Array of options with $key set.
assetTimestamp()
Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in Configure. If Asset.timestamp is true and debug is true, or Asset.timestamp === 'force' a timestamp will be added.
public
assetTimestamp(string $path[, string|bool $timestamp = null ]) : string
Parameters
- $path : string
-
The file path to timestamp, the path must be inside
App.wwwRoot
in Configure. - $timestamp : string|bool = null
-
If set will overrule the value of
Asset.timestamp
in Configure.
Return values
string —Path with a timestamp added, or not.
assetUrl()
Generates URL for given asset file.
public
assetUrl(string $path[, array<string, mixed> $options = [] ]) : string
Depending on options passed provides full URL with domain name. Also calls
Helper::assetTimestamp()
to add timestamp to local files.
Options:
-
fullBase
Boolean true or a string (e.g. https://example) to return full URL with protocol and domain name. -
pathPrefix
Path prefix for relative URLs -
ext
Asset extension to append -
plugin
False value will prevent parsing path as a plugin -
timestamp
Overrides the value ofAsset.timestamp
in Configure. Set to false to skip timestamp generation. Set to true to apply timestamps when debug is true. Set to 'force' to always enable timestamping regardless of debug value.
Parameters
- $path : string
-
Path string or URL array
- $options : array<string, mixed> = []
-
Options array.
Return values
string —Generated URL
build()
Returns a URL based on provided parameters.
public
build([array<string|int, mixed>|string|null $url = null ][, array<string, mixed> $options = [] ]) : string
Options:
-
escape
: If false, the URL will be returned unescaped, do only use if it is manually escaped afterwards before being displayed. -
fullBase
: If true, the full base URL will be prepended to the result
Parameters
- $url : array<string|int, mixed>|string|null = null
-
Either a relative string URL like
/products/view/23
or an array of URL parameters. Using an array for URLs will allow you to leverage the reverse routing features of CakePHP. - $options : array<string, mixed> = []
-
Array of options.
Return values
string —Full translated URL with base path.
buildFromPath()
Returns a URL from a route path string.
public
buildFromPath(string $path[, array<string|int, mixed> $params = [] ][, array<string, mixed> $options = [] ]) : string
Options:
-
escape
: If false, the URL will be returned unescaped, do only use if it is manually escaped afterwards before being displayed. -
fullBase
: If true, the full base URL will be prepended to the result
Parameters
- $path : string
-
Cake-relative route path.
- $params : array<string|int, mixed> = []
-
An array specifying any additional parameters. Can be also any special parameters supported by
Router::url()
. - $options : array<string, mixed> = []
-
Array of options.
Tags
Return values
string —Full translated URL with base path.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thiscss()
Generates URL for given CSS file.
public
css(string $path[, array<string, mixed> $options = [] ]) : string
Depending on options passed provides full URL with domain name. Also calls
Helper::assetTimestamp()
to add timestamp to local files.
Parameters
- $path : string
-
Path string.
- $options : array<string, mixed> = []
-
Options array. Possible keys:
fullBase
Return full URL with domain namepathPrefix
Path prefix for relative URLsext
Asset extension to appendplugin
False value will prevent parsing path as a plugintimestamp
Overrides the value ofAsset.timestamp
in Configure. Set to false to skip timestamp generation. Set to true to apply timestamps when debug is true. Set to 'force' to always enable timestamping regardless of debug value.
Return values
string —Generated URL
getConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getView()
Get the view instance this helper is bound to.
public
getView() : View
Return values
View —The bound view instance.
image()
Generates URL for given image file.
public
image(string $path[, array<string, mixed> $options = [] ]) : string
Depending on options passed provides full URL with domain name. Also calls
Helper::assetTimestamp()
to add timestamp to local files.
Parameters
- $path : string
-
Path string.
- $options : array<string, mixed> = []
-
Options array. Possible keys:
fullBase
Return full URL with domain namepathPrefix
Path prefix for relative URLsplugin
False value will prevent parsing path as a plugintimestamp
Overrides the value ofAsset.timestamp
in Configure. Set to false to skip timestamp generation. Set to true to apply timestamps when debug is true. Set to 'force' to always enable timestamping regardless of debug value.
Return values
string —Generated URL
implementedEvents()
Event listeners.
public
implementedEvents() : array<string, mixed>
Return values
array<string, mixed>initialize()
Check proper configuration
public
initialize(array<string, mixed> $config) : void
Parameters
- $config : array<string, mixed>
-
The configuration settings provided to this helper.
script()
Generates URL for given javascript file.
public
script(string $path[, array<string, mixed> $options = [] ]) : string
Depending on options passed provides full URL with domain name. Also calls
Helper::assetTimestamp()
to add timestamp to local files.
Parameters
- $path : string
-
Path string.
- $options : array<string, mixed> = []
-
Options array. Possible keys:
fullBase
Return full URL with domain namepathPrefix
Path prefix for relative URLsext
Asset extension to appendplugin
False value will prevent parsing path as a plugintimestamp
Overrides the value ofAsset.timestamp
in Configure. Set to false to skip timestamp generation. Set to true to apply timestamps when debug is true. Set to 'force' to always enable timestamping regardless of debug value.
Return values
string —Generated URL
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thiswebroot()
Checks if a file exists when theme is used, if no file is found default location is returned
public
webroot(string $file) : string
Parameters
- $file : string
-
The file to create a webroot path to.
Return values
string —Web accessible path to file.
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_confirm()
Returns a string to be used as onclick handler for confirm dialogs.
protected
_confirm(string $okCode, string $cancelCode) : string
Parameters
- $okCode : string
-
Code to be executed after user chose 'OK'
- $cancelCode : string
-
Code to be executed after user chose 'Cancel'
Return values
string —"onclick" JS code