PHP Class yii\web\UrlManager

UrlManager is configured as an application component in Application by default. You can access that instance via Yii::$app->urlManager. You can modify its configuration by adding an array to your application config under components as it is shown in the following example: php 'urlManager' => [ 'enablePrettyUrl' => true, 'rules' => [ your rules go here ], ... ]
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Component
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$cache the cache object or the application component ID of the cache object. Compiled URL rules will be cached through this cache object, if it is available. After the UrlManager object is created, if you want to change this property, you should only assign it with a cache object. Set this property to false if you do not want to cache the URL rules.
$enablePrettyUrl whether to enable pretty URLs. Instead of putting all parameters in the query string part of a URL, pretty URLs allow using path info to represent some of the parameters and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of "/index.php?r=news%2Fview&id=100".
$enableStrictParsing whether to enable strict parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the [[rules]] in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route. This property is used only when [[enablePrettyUrl]] is true.
$normalizer the configuration for UrlNormalizer used by this UrlManager. The default value is false, which means normalization will be skipped. If you wish to enable URL normalization, you should configure this property manually. For example: php [ 'class' => 'yii\web\UrlNormalizer', 'collapseSlashes' => true, 'normalizeTrailingSlash' => true, ]
$routeParam the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is false.
$ruleConfig the default configuration of URL rules. Individual rule configurations specified via [[rules]] will take precedence when the same property of the rule is configured.
$rules the rules for creating and parsing URLs when [[enablePrettyUrl]] is true. This property is used only if [[enablePrettyUrl]] is true. Each element in the array is the configuration array for creating a single URL rule. The configuration will be merged with [[ruleConfig]] first before it is used for creating the rule object. A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]] and [[UrlRule::route|route]]: 'pattern' => 'route'. That is, instead of using a configuration array, one can use the key to represent the pattern and the value the corresponding route. For example, 'post/' => 'post/view'. For RESTful routing the mentioned shortcut format also allows you to specify the [[UrlRule::verb|HTTP verb]] that the rule should apply for. You can do that by prepending it to the pattern, separated by space. For example, 'PUT post/' => 'post/update'. You may specify multiple verbs by separating them with comma like this: 'POST,PUT post/index' => 'post/create'. The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE. Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way so you normally would not specify a verb for normal GET request. Here is an example configuration for RESTful CRUD controller: php [ 'dashboard' => 'site/index', 'POST s' => '/create', 's' => '/index', 'PUT /' => '/update', 'DELETE /' => '/delete', '/' => '/view', ]; Note that if you modify this property after the UrlManager object is created, make sure you populate the array with rule objects instead of rule configurations.
$showScriptName whether to show entry script name in the constructed URL. Defaults to true. This property is used only if [[enablePrettyUrl]] is true.
$suffix the URL suffix used when [[enablePrettyUrl]] is true. For example, ".html" can be used so that the URL looks like pointing to a static HTML page. This property is used only if [[enablePrettyUrl]] is true.

Protected Properties

Property Type Description
$cacheKey the cache key for cached rules

Public Methods

Method Description
addRules ( array $rules, boolean $append = true ) Adds additional URL rules.
createAbsoluteUrl ( string | array $params, string | null $scheme = null ) : string Creates an absolute URL using the given route and query parameters.
createUrl ( string | array $params ) : string Creates a URL using the given route and query parameters.
getBaseUrl ( ) : string Returns the base URL that is used by UrlManager::createUrl to prepend to created URLs.
getHostInfo ( ) : string Returns the host info that is used by UrlManager::createAbsoluteUrl to prepend to created URLs.
getScriptUrl ( ) : string Returns the entry script URL that is used by UrlManager::createUrl to prepend to created URLs.
init ( ) Initializes UrlManager.
parseRequest ( Request $request ) : array | boolean Parses the user request.
setBaseUrl ( string $value ) Sets the base URL that is used by UrlManager::createUrl to prepend to created URLs.
setHostInfo ( string $value ) Sets the host info that is used by UrlManager::createAbsoluteUrl to prepend to created URLs.
setScriptUrl ( string $value ) Sets the entry script URL that is used by UrlManager::createUrl to prepend to created URLs.

Protected Methods

Method Description
buildRules ( array $rules ) : yii\web\UrlRuleInterface[] Builds URL rule objects from the given rule declarations.
getUrlFromCache ( string $cacheKey, string $route, array $params ) : boolean | string Get URL from internal cache if exists
setRuleToCache ( $cacheKey, yii\web\UrlRuleInterface $rule ) Store rule (e.g. UrlRule) to internal cache

Method Details

addRules() public method

This method will call UrlManager::buildRules to parse the given rule declarations and then append or insert them to the existing [[rules]]. Note that if [[enablePrettyUrl]] is false, this method will do nothing.
public addRules ( array $rules, boolean $append = true )
$rules array the new rules to be added. Each array element represents a single rule declaration. Please refer to [[rules]] for the acceptable rule format.
$append boolean whether to add the new rules by appending them to the end of the existing rules.

buildRules() protected method

Builds URL rule objects from the given rule declarations.
protected buildRules ( array $rules ) : yii\web\UrlRuleInterface[]
$rules array the rule declarations. Each array element represents a single rule declaration. Please refer to [[rules]] for the acceptable rule formats.
return yii\web\UrlRuleInterface[] the rule objects built from the given rule declarations

createAbsoluteUrl() public method

This method prepends the URL created by UrlManager::createUrl with the [[hostInfo]]. Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route as an absolute route.
See also: createUrl()
public createAbsoluteUrl ( string | array $params, string | null $scheme = null ) : string
$params string | array use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
$scheme string | null the scheme to use for the URL (either `http`, `https` or empty string for protocol-relative URL). If not specified the scheme of the current request will be used.
return string the created URL

createUrl() public method

You may specify the route as a string, e.g., site/index. You may also use an array if you want to specify additional query parameters for the URL being created. The array format must be: php generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2 ['site/index', 'param1' => 'value1', 'param2' => 'value2'] If you want to create a URL with an anchor, you can use the array format with a # parameter. For example, php generates: /index.php?r=site%2Findex¶m1=value1#name ['site/index', 'param1' => 'value1', '#' => 'name'] The URL created is a relative one. Use UrlManager::createAbsoluteUrl to create an absolute URL. Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route as an absolute route.
public createUrl ( string | array $params ) : string
$params string | array use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
return string the created URL

getBaseUrl() public method

It defaults to [[Request::baseUrl]]. This is mainly used when [[enablePrettyUrl]] is true and [[showScriptName]] is false.
public getBaseUrl ( ) : string
return string the base URL that is used by [[createUrl()]] to prepend to created URLs.

getHostInfo() public method

Returns the host info that is used by UrlManager::createAbsoluteUrl to prepend to created URLs.
public getHostInfo ( ) : string
return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs.

getScriptUrl() public method

It defaults to [[Request::scriptUrl]]. This is mainly used when [[enablePrettyUrl]] is false or [[showScriptName]] is true.
public getScriptUrl ( ) : string
return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs.

getUrlFromCache() protected method

Get URL from internal cache if exists
See also: createUrl()
Since: 2.0.8
protected getUrlFromCache ( string $cacheKey, string $route, array $params ) : boolean | string
$cacheKey string generated cache key to store data.
$route string the route (e.g. `site/index`).
$params array rule params.
return boolean | string the created URL

init() public method

Initializes UrlManager.
public init ( )

parseRequest() public method

Parses the user request.
public parseRequest ( Request $request ) : array | boolean
$request Request the request component
return array | boolean the route and the associated parameters. The latter is always empty if [[enablePrettyUrl]] is `false`. `false` is returned if the current request cannot be successfully parsed.

setBaseUrl() public method

This is mainly used when [[enablePrettyUrl]] is true and [[showScriptName]] is false.
public setBaseUrl ( string $value )
$value string the base URL that is used by [[createUrl()]] to prepend to created URLs.

setHostInfo() public method

Sets the host info that is used by UrlManager::createAbsoluteUrl to prepend to created URLs.
public setHostInfo ( string $value )
$value string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs.

setRuleToCache() protected method

Store rule (e.g. UrlRule) to internal cache
Since: 2.0.8
protected setRuleToCache ( $cacheKey, yii\web\UrlRuleInterface $rule )
$cacheKey
$rule yii\web\UrlRuleInterface

setScriptUrl() public method

This is mainly used when [[enablePrettyUrl]] is false or [[showScriptName]] is true.
public setScriptUrl ( string $value )
$value string the entry script URL that is used by [[createUrl()]] to prepend to created URLs.

Property Details

$cache public_oe property

the cache object or the application component ID of the cache object. Compiled URL rules will be cached through this cache object, if it is available. After the UrlManager object is created, if you want to change this property, you should only assign it with a cache object. Set this property to false if you do not want to cache the URL rules.
public $cache

$cacheKey protected_oe property

the cache key for cached rules
Since: 2.0.8
protected $cacheKey

$enablePrettyUrl public_oe property

whether to enable pretty URLs. Instead of putting all parameters in the query string part of a URL, pretty URLs allow using path info to represent some of the parameters and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of "/index.php?r=news%2Fview&id=100".
public $enablePrettyUrl

$enableStrictParsing public_oe property

whether to enable strict parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the [[rules]] in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route. This property is used only when [[enablePrettyUrl]] is true.
public $enableStrictParsing

$normalizer public_oe property

the configuration for UrlNormalizer used by this UrlManager. The default value is false, which means normalization will be skipped. If you wish to enable URL normalization, you should configure this property manually. For example: php [ 'class' => 'yii\web\UrlNormalizer', 'collapseSlashes' => true, 'normalizeTrailingSlash' => true, ]
Since: 2.0.10
public $normalizer

$routeParam public_oe property

the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is false.
public $routeParam

$ruleConfig public_oe property

the default configuration of URL rules. Individual rule configurations specified via [[rules]] will take precedence when the same property of the rule is configured.
public $ruleConfig

$rules public_oe property

the rules for creating and parsing URLs when [[enablePrettyUrl]] is true. This property is used only if [[enablePrettyUrl]] is true. Each element in the array is the configuration array for creating a single URL rule. The configuration will be merged with [[ruleConfig]] first before it is used for creating the rule object. A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]] and [[UrlRule::route|route]]: 'pattern' => 'route'. That is, instead of using a configuration array, one can use the key to represent the pattern and the value the corresponding route. For example, 'post/' => 'post/view'. For RESTful routing the mentioned shortcut format also allows you to specify the [[UrlRule::verb|HTTP verb]] that the rule should apply for. You can do that by prepending it to the pattern, separated by space. For example, 'PUT post/' => 'post/update'. You may specify multiple verbs by separating them with comma like this: 'POST,PUT post/index' => 'post/create'. The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE. Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way so you normally would not specify a verb for normal GET request. Here is an example configuration for RESTful CRUD controller: php [ 'dashboard' => 'site/index', 'POST s' => '/create', 's' => '/index', 'PUT /' => '/update', 'DELETE /' => '/delete', '/' => '/view', ]; Note that if you modify this property after the UrlManager object is created, make sure you populate the array with rule objects instead of rule configurations.
public $rules

$showScriptName public_oe property

whether to show entry script name in the constructed URL. Defaults to true. This property is used only if [[enablePrettyUrl]] is true.
public $showScriptName

$suffix public_oe property

the URL suffix used when [[enablePrettyUrl]] is true. For example, ".html" can be used so that the URL looks like pointing to a static HTML page. This property is used only if [[enablePrettyUrl]] is true.
public $suffix