Class yii\web\UrlRule

Inheritanceyii\web\UrlRule » yii\base\Object
Implementsyii\base\Configurable, yii\web\UrlRuleInterface
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/web/UrlRule.php

UrlRule represents a rule used by yii\web\UrlManager for parsing and generating URLs.

To define your own URL parsing and creation logic you can extend from this class and add it to yii\web\UrlManager::$rules like this:

'rules' => [
    ['class' => 'MyUrlRule', 'pattern' => '...', 'route' => 'site/index', ...],
    // ...
]

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$defaults array The default GET parameters (name => value) that this rule provides. yii\web\UrlRule
$encodeParams boolean A value indicating if parameters should be url encoded. yii\web\UrlRule
$host string The pattern used to parse and create the host info part of a URL (e.g. `http://example. yii\web\UrlRule
$mode integer A value indicating if this rule should be used for both request parsing and URL creation, parsing only, or creation only. yii\web\UrlRule
$name string The name of this rule. yii\web\UrlRule
$normalizer yii\web\UrlNormalizer|array|false|null The configuration for yii\web\UrlNormalizer used by this rule. yii\web\UrlRule
$pattern string The pattern used to parse and create the path info part of a URL. yii\web\UrlRule
$route string The route to the controller action yii\web\UrlRule
$suffix string The URL suffix used for this rule. yii\web\UrlRule
$verb string|array The HTTP verb (e.g. GET, POST, DELETE) that this rule should match. yii\web\UrlRule

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$placeholders array List of placeholders for matching parameters names. yii\web\UrlRule

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__toString() yii\web\UrlRule
__unset() Sets an object property to null. yii\base\Object
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
createUrl() Creates a URL according to the given route and parameters. yii\web\UrlRule
hasMethod() Returns a value indicating whether a method is defined. yii\base\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes this rule. yii\web\UrlRule
parseRequest() Parses the given request and returns the corresponding route and parameters. yii\web\UrlRule

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getNormalizer() yii\web\UrlRule
getParamRules() Returns list of regex for matching parameter. yii\web\UrlRule
hasNormalizer() yii\web\UrlRule
substitutePlaceholderNames() Iterates over $placeholders and checks whether each placeholder exists as a key in $matches array. yii\web\UrlRule

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
CREATION_ONLY 2 Set $mode with this value to mark that this rule is for URL creation only yii\web\UrlRule
PARSING_ONLY 1 Set $mode with this value to mark that this rule is for URL parsing only yii\web\UrlRule

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 array $defaults = []
$encodeParams public property

A value indicating if parameters should be url encoded.

public boolean $encodeParams true
$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 string $host null
$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 integer $mode null
$name public property

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

public string $name null
$normalizer public property (available since version 2.0.10)

The configuration for yii\web\UrlNormalizer used by this rule. If null, yii\web\UrlManager::$normalizer will be used, if false, normalization will be skipped for this rule.

$pattern public property

The pattern used to parse and create the path info part of a URL.

See also:

public string $pattern null
$placeholders protected property (available since version 2.0.7)

List of placeholders for matching parameters names. Used in parseRequest(), 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:

protected array $placeholders = []
$route public property

The route to the controller action

public string $route null
$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 set, the value of yii\web\UrlManager::$suffix will be used.

public string $suffix null
$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 string|array $verb null

Method Details

__toString() public method (available since version 2.0.11)

public string __toString ( )
createUrl() public method

Creates a URL according to the given route and parameters.

public string|boolean createUrl ( $manager, $route, $params )
$manager yii\web\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 (available since version 2.0.10)

protected yii\web\UrlNormalizer|null getNormalizer ( $manager )
$manager yii\web\UrlManager

The URL manager

getParamRules() protected method (available since version 2.0.6)

Returns list of regex for matching parameter.

protected array getParamRules ( )
return array

Parameter keys and regexp rules.

hasNormalizer() protected method (available since version 2.0.10)

protected boolean hasNormalizer ( $manager )
$manager yii\web\UrlManager

The URL manager

init() public method

Initializes this rule.

public void init ( )
parseRequest() public method

Parses the given request and returns the corresponding route and parameters.

public array|boolean parseRequest ( $manager, $request )
$manager yii\web\UrlManager

The URL manager

$request yii\web\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 (available since version 2.0.7)

Iterates over $placeholders and checks whether each placeholder exists as a key in $matches array.

When found - replaces this placeholder key with a appropriate name of matching parameter. Used in parseRequest(), createUrl().

See also $placeholders.

protected array substitutePlaceholderNames ( array $matches )
$matches array

Result of preg_match() call

return array

Input array with replaced placeholder keys