Skip to content
PHP Ecosystem
Explore

UrlRule

API reference › yii › web › UrlRule

classyii\web\UrlRule
Extendsyii\base\BaseObject
ImplementsUrlRuleInterface, yii\base\Configurable
Since2.0
Sourceframework/web/UrlRule.php

UrlRule represents a rule used by 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 UrlManager::$rules like this:

'rules' => [
['class' => 'MyUrlRule', 'pattern' => '...', 'route' => 'site/index', ...],
// ...
]
PropertyTypeDescriptionDefined by
$createUrlStatus read-onlyint|nullStatus of the URL creation after the last createUrl() call. null if rule does not provide info about create status.UrlRule
$defaultsarrayThe default GET parameters (name => value) that this rule provides. Default: [].UrlRule
$encodeParamsboolA value indicating if parameters should be url encoded. Default: true.UrlRule
$hoststring|nullThe pattern used to parse and create the host info part of a URL (e.g. https://example.com). Default: null.UrlRule
$modeint|nullA value indicating if this rule should be used for both request parsing and URL creation, parsing only, or creation only. Default: null.UrlRule
$namestring|nullThe name of this rule. Default: null.UrlRule
$normalizerUrlNormalizer|array|false|nullThe configuration for UrlNormalizer used by this rule. Default: null. Available since 2.0.10.UrlRule
$patternstringThe pattern used to parse and create the path info part of a URL. Default: null.UrlRule
$routestringThe route to the controller action Default: null.UrlRule
$suffixstring|nullThe URL suffix used for this rule. Default: null.UrlRule
$verbstring|array|nullThe HTTP verb (e.g. Default: null.UrlRule
$createStatus protectedint|nullStatus of the URL creation after the last createUrl() call. Default: null. Available since 2.0.12.UrlRule
$placeholders protectedarrayList of placeholders for matching parameters names. Default: []. Available since 2.0.7.UrlRule
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__toString()UrlRule
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
createUrl()Creates a URL according to the given route and parameters.UrlRule
getCreateUrlStatus()Returns status of the URL creation after the last createUrl() call.UrlRule
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes this rule.UrlRule
parseRequest()Parses the given request and returns the corresponding route and parameters.UrlRule
getNormalizer() protectedUrlRule
getParamRules() protectedReturns list of regex for matching parameter.UrlRule
hasNormalizer() protectedUrlRule
substitutePlaceholderNames() protectedIterates over $placeholders and checks whether each placeholder exists as a key in $matches array.UrlRule
ConstantValueDescriptionDefined by
PARSING_ONLY1Set $mode with this value to mark that this rule is for URL parsing only.UrlRule
CREATION_ONLY2Set $mode with this value to mark that this rule is for URL creation only.UrlRule
CREATE_STATUS_SUCCESS0Represents the successful URL generation by last createUrl() call. Available since 2.0.12.UrlRule
CREATE_STATUS_PARSING_ONLY1Represents the unsuccessful URL generation by last createUrl() call, because rule does not support creating URLs. Available since 2.0.12.UrlRule
CREATE_STATUS_ROUTE_MISMATCH2Represents the unsuccessful URL generation by last createUrl() call, because of mismatched route. Available since 2.0.12.UrlRule
CREATE_STATUS_PARAMS_MISMATCH4Represents the unsuccessful URL generation by last createUrl() call, because of mismatched or missing parameters. Available since 2.0.12.UrlRule
public array $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.

public int|null $mode = null

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 string|null $name = null

The name of this rule.

If not set, it will use $pattern as the name.

public yii\web\UrlNormalizer|array|false|null $normalizer = null

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 string|null $suffix = null

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 UrlManager::$suffix will be used. Default values should be strings. Non-string values will be automatically converted to strings for comparison with URL parameters.

public string|array|null $verb = null

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 array $placeholders = []

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.

Since

2.0.7

public function __toString(): string

Returns

string

Since

2.0.11

public function createUrl(yii\web\UrlManager $manager, string $route, array $params): string|bool

Creates a URL according to the given route and parameters.

Parameters

ParameterTypeDescription
$managerUrlManagerThe URL manager
$routestringThe route. It should not have slashes at the beginning or the end.
$paramsarrayThe parameters

Returns

string|bool: The created URL, or false if this rule cannot be used for creating this URL.

public function getCreateUrlStatus(): int|null

Returns status of the URL creation after the last createUrl() call.

Returns

int|null: Status of the URL creation after the last createUrl() call. null if rule does not provide info about create status.

Since

2.0.12

See also

public function init(): mixed

Initializes this rule.

Returns

mixed

public function parseRequest(yii\web\UrlManager $manager, yii\web\Request $request): array|bool

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

Parameters

ParameterTypeDescription
$managerUrlManagerThe URL manager
$requestRequestThe request component

Returns

array|bool: 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.

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

Parameters

ParameterTypeDescription
$managerUrlManagerThe URL manager

Returns

UrlNormalizer|null

Since

2.0.10

protected function getParamRules(): array

Returns list of regex for matching parameter.

Returns

array: Parameter keys and regexp rules.

Since

2.0.6

protected function hasNormalizer(yii\web\UrlManager $manager): bool

Parameters

ParameterTypeDescription
$managerUrlManagerThe URL manager

Returns

bool

Since

2.0.10

protected function substitutePlaceholderNames(array $matches): array

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().

Parameters

ParameterTypeDescription
$matchesarrayResult of preg_match() call

Returns

array: Input array with replaced placeholder keys

Since

2.0.7

See also