PHP Class yii\web\UrlRule

To define your own URL parsing and creation logic you can extend from this class and add it to [[UrlManager::rules]] like this: ~~~ 'rules' => [ ['class' => 'MyUrlRule', 'pattern' => '...', 'route' => 'site/index', ...], ... ] ~~~
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object, implements yii\web\UrlRuleInterface
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$defaults the default GET parameters (name => value) that this rule provides. When this rule is used to parse the incoming request, the values declared in this property will be injected into $_GET.
$encodeParams a value indicating if parameters should be url encoded.
$host the pattern used to parse and create the host info part of a URL (e.g. http://example.com).
$mode a value indicating if this rule should be used for both request parsing and URL creation, parsing only, or creation only. If not set or 0, it means the rule is both request parsing and URL creation. If it is [[PARSING_ONLY]], the rule is for request parsing only. If it is [[CREATION_ONLY]], the rule is for URL creation only.
$name the name of this rule. If not set, it will use [[pattern]] as the name.
$normalizer the configuration for UrlNormalizer used by this rule. If null, [[UrlManager::normalizer]] will be used, if false, normalization will be skipped for this rule.
$pattern On the rule initialization, the [[pattern]] matching parameters names will be replaced with [[placeholders]].
$route the route to the controller action
$suffix the URL suffix used for this rule. For example, ".html" can be used so that the URL looks like pointing to a static HTML page. If not, the value of [[UrlManager::suffix]] will be used.
$verb the HTTP verb (e.g. GET, POST, DELETE) that this rule should match. Use array to represent multiple verbs that this rule may match. If this property is not set, the rule can match any verb. Note that this property is only used when parsing a request. It is ignored for URL creation.

Protected Properties

Property Type Description
$placeholders list of placeholders for matching parameters names. Used in UrlRule::parseRequest, UrlRule::createUrl. On the rule initialization, the [[pattern]] parameters names will be replaced with placeholders. This array contains relations between the original parameters names and their placeholders. The array keys are the placeholders and the values are the original names.

Public Methods

Method Description
createUrl ( UrlManager $manager, string $route, array $params ) : string | boolean Creates a URL according to the given route and parameters.
init ( ) Initializes this rule.
parseRequest ( UrlManager $manager, Request $request ) : array | boolean Parses the given request and returns the corresponding route and parameters.

Protected Methods

Method Description
getNormalizer ( UrlManager $manager ) : UrlNormalizer | null
getParamRules ( ) : array Returns list of regex for matching parameter.
hasNormalizer ( UrlManager $manager ) : boolean
substitutePlaceholderNames ( array $matches ) : array Iterates over [[placeholders]] and checks whether each placeholder exists as a key in $matches array.

Method Details

createUrl() public method

Creates a URL according to the given route and parameters.
public createUrl ( UrlManager $manager, string $route, array $params ) : string | boolean
$manager UrlManager the URL manager
$route string the route. It should not have slashes at the beginning or the end.
$params array the parameters
return string | boolean the created URL, or `false` if this rule cannot be used for creating this URL.

getNormalizer() protected method

Since: 2.0.10
protected getNormalizer ( UrlManager $manager ) : UrlNormalizer | null
$manager UrlManager the URL manager
return UrlNormalizer | null

getParamRules() protected method

Returns list of regex for matching parameter.
Since: 2.0.6
protected getParamRules ( ) : array
return array parameter keys and regexp rules.

hasNormalizer() protected method

Since: 2.0.10
protected hasNormalizer ( UrlManager $manager ) : boolean
$manager UrlManager the URL manager
return boolean

init() public method

Initializes this rule.
public init ( )

parseRequest() public method

Parses the given request and returns the corresponding route and parameters.
public parseRequest ( UrlManager $manager, Request $request ) : array | boolean
$manager UrlManager the URL manager
$request Request the request component
return array | boolean the parsing result. The route and the parameters are returned as an array. If `false`, it means this rule cannot be used to parse this path info.

substitutePlaceholderNames() protected method

When found - replaces this placeholder key with a appropriate name of matching parameter. Used in UrlRule::parseRequest, UrlRule::createUrl.
See also: placeholders
Since: 2.0.7
protected substitutePlaceholderNames ( array $matches ) : array
$matches array result of `preg_match()` call
return array input array with replaced placeholder keys

Property Details

$defaults public property

the default GET parameters (name => value) that this rule provides. When this rule is used to parse the incoming request, the values declared in this property will be injected into $_GET.
public $defaults

$encodeParams public property

a value indicating if parameters should be url encoded.
public $encodeParams

$host public property

the pattern used to parse and create the host info part of a URL (e.g. http://example.com).
See also: pattern
public $host

$mode public property

a value indicating if this rule should be used for both request parsing and URL creation, parsing only, or creation only. If not set or 0, it means the rule is both request parsing and URL creation. If it is [[PARSING_ONLY]], the rule is for request parsing only. If it is [[CREATION_ONLY]], the rule is for URL creation only.
public $mode

$name public property

the name of this rule. If not set, it will use [[pattern]] as the name.
public $name

$normalizer public property

the configuration for UrlNormalizer used by this rule. If null, [[UrlManager::normalizer]] will be used, if false, normalization will be skipped for this rule.
Since: 2.0.10
public $normalizer

$pattern public property

On the rule initialization, the [[pattern]] matching parameters names will be replaced with [[placeholders]].
See also: host
See also: placeholders
public $pattern

$placeholders protected property

list of placeholders for matching parameters names. Used in UrlRule::parseRequest, UrlRule::createUrl. On the rule initialization, the [[pattern]] parameters names will be replaced with placeholders. This array contains relations between the original parameters names and their placeholders. The array keys are the placeholders and the values are the original names.
See also: parseRequest()
See also: createUrl()
Since: 2.0.7
protected $placeholders

$route public property

the route to the controller action
public $route

$suffix public property

the URL suffix used for this rule. For example, ".html" can be used so that the URL looks like pointing to a static HTML page. If not, the value of [[UrlManager::suffix]] will be used.
public $suffix

$verb public property

the HTTP verb (e.g. GET, POST, DELETE) that this rule should match. Use array to represent multiple verbs that this rule may match. If this property is not set, the rule can match any verb. Note that this property is only used when parsing a request. It is ignored for URL creation.
public $verb