UrlRule
API reference › yii › web › UrlRule
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', ...], // ...]Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$createUrlStatus read-only | int| | Status of the URL creation after the last createUrl() call. null if rule does not provide info about create status. | UrlRule |
$defaults | array | The default GET parameters (name => value) that this rule provides. Default: []. | UrlRule |
$encodeParams | bool | A value indicating if parameters should be url encoded. Default: true. | UrlRule |
$host | string| | The pattern used to parse and create the host info part of a URL (e.g. https://example.com). Default: null. | UrlRule |
$mode | int| | A value indicating if this rule should be used for both request parsing and URL creation, parsing only, or creation only. Default: null. | UrlRule |
$name | string| | The name of this rule. Default: null. | UrlRule |
$normalizer | UrlNormalizer| | The configuration for UrlNormalizer used by this rule. Default: null. Available since 2.0.10. | UrlRule |
$pattern | string | The pattern used to parse and create the path info part of a URL. Default: null. | UrlRule |
$route | string | The route to the controller action Default: null. | UrlRule |
$suffix | string| | The URL suffix used for this rule. Default: null. | UrlRule |
$verb | string| | The HTTP verb (e.g. Default: null. | UrlRule |
$createStatus protected | int| | Status of the URL creation after the last createUrl() call. Default: null. Available since 2.0.12. | UrlRule |
$placeholders protected | array | List of placeholders for matching parameters names. Default: []. Available since 2.0.7. | UrlRule |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__toString() | UrlRule | |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
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\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes this rule. | UrlRule |
parseRequest() | Parses the given request and returns the corresponding route and parameters. | UrlRule |
getNormalizer() protected | UrlRule | |
getParamRules() protected | Returns list of regex for matching parameter. | UrlRule |
hasNormalizer() protected | UrlRule | |
substitutePlaceholderNames() protected | Iterates over $placeholders and checks whether each placeholder exists as a key in $matches array. | UrlRule |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
PARSING_ONLY | 1 | Set $mode with this value to mark that this rule is for URL parsing only. | UrlRule |
CREATION_ONLY | 2 | Set $mode with this value to mark that this rule is for URL creation only. | UrlRule |
CREATE_STATUS_SUCCESS | 0 | Represents the successful URL generation by last createUrl() call. Available since 2.0.12. | UrlRule |
CREATE_STATUS_PARSING_ONLY | 1 | Represents the unsuccessful URL generation by last createUrl() call, because rule does not support creating URLs. Available since 2.0.12. | UrlRule |
CREATE_STATUS_ROUTE_MISMATCH | 2 | Represents the unsuccessful URL generation by last createUrl() call, because of mismatched route. Available since 2.0.12. | UrlRule |
CREATE_STATUS_PARAMS_MISMATCH | 4 | Represents the unsuccessful URL generation by last createUrl() call, because of mismatched or missing parameters. Available since 2.0.12. | UrlRule |
Property details
Section titled “Property details”$defaults
Section titled “$defaults ”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.
$mode
Section titled “$mode ”public int|null $mode = nullA 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
Section titled “$name ”public string|null $name = nullThe name of this rule.
If not set, it will use $pattern as the name.
$normalizer
Section titled “$normalizer ”public yii\web\UrlNormalizer|array|false|null $normalizer = nullThe 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
$suffix
Section titled “$suffix ”public string|null $suffix = nullThe 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.
$verb
Section titled “$verb ”public string|array|null $verb = nullThe 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.
$placeholders
Section titled “$placeholders ”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
Method details
Section titled “Method details”__toString()
Section titled “__toString() ”public function __toString(): stringReturns
string
Since
2.0.11
createUrl()
Section titled “createUrl() ”public function createUrl(yii\web\UrlManager $manager, string $route, array $params): string|boolCreates a URL according to the given route and parameters.
Parameters
| Parameter | Type | Description |
|---|---|---|
$manager | UrlManager | The URL manager |
$route | string | The route. It should not have slashes at the beginning or the end. |
$params | array | The parameters |
Returns
string|bool: The created URL, or false if this rule cannot be used for creating this URL.
getCreateUrlStatus()
Section titled “getCreateUrlStatus() ”public function getCreateUrlStatus(): int|nullReturns 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
parseRequest()
Section titled “parseRequest() ”public function parseRequest(yii\web\UrlManager $manager, yii\web\Request $request): array|boolParses the given request and returns the corresponding route and parameters.
Parameters
| Parameter | Type | Description |
|---|---|---|
$manager | UrlManager | The URL manager |
$request | Request | The 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.
getNormalizer()
Section titled “getNormalizer() ”protected function getNormalizer(yii\web\UrlManager $manager): yii\web\UrlNormalizer|nullParameters
| Parameter | Type | Description |
|---|---|---|
$manager | UrlManager | The URL manager |
Returns
UrlNormalizer|null
Since
2.0.10
getParamRules()
Section titled “getParamRules() ”protected function getParamRules(): arrayReturns list of regex for matching parameter.
Returns
array: Parameter keys and regexp rules.
Since
2.0.6
hasNormalizer()
Section titled “hasNormalizer() ”protected function hasNormalizer(yii\web\UrlManager $manager): boolsubstitutePlaceholderNames()
Section titled “substitutePlaceholderNames() ”protected function substitutePlaceholderNames(array $matches): arrayIterates 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
| Parameter | Type | Description |
|---|---|---|
$matches | array | Result of preg_match() call |
Returns
array: Input array with replaced placeholder keys
Since
2.0.7
See also