GroupUrlRule
API reference › yii › web › GroupUrlRule
GroupUrlRule represents a collection of URL rules sharing the same prefix in their patterns and routes.
GroupUrlRule is best used by a module which often uses module ID as the prefix for the URL rules.
For example, the following code creates a rule for the admin module:
new GroupUrlRule([ 'prefix' => 'admin', 'rules' => [ 'login' => 'user/login', 'logout' => 'user/logout', 'dashboard' => 'default/dashboard', ],]);
// the above rule is equivalent to the following three rules:
[ 'admin/login' => 'admin/user/login', 'admin/logout' => 'admin/user/logout', 'admin/dashboard' => 'admin/default/dashboard',]The above example assumes the prefix for patterns and routes are the same. They can be made different
by configuring $prefix and $routePrefix separately.
Using a GroupUrlRule is more efficient than directly declaring the individual rules it contains. This is because GroupUrlRule can quickly determine if it should process a URL parsing or creation request by simply checking if the prefix matches.
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. | CompositeUrlRule |
$prefix | string | The prefix for the pattern part of every rule declared in $rules. Default: null. | GroupUrlRule |
$routePrefix | string| | The prefix for the route part of every rule declared in $rules. Default: null. | GroupUrlRule |
$ruleConfig | array | The default configuration of URL rules. Default: ['class' => 'yii\. | GroupUrlRule |
$rules | UrlRuleInterface[]| | The rules contained within this composite rule. Default: []. | GroupUrlRule |
$createStatus protected | int| | Status of the URL creation after the last createUrl() call. Default: null. Available since 2.0.12. | CompositeUrlRule |
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\ |
__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. | GroupUrlRule |
getCreateUrlStatus() | Returns status of the URL creation after the last createUrl() call. | CompositeUrlRule |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | GroupUrlRule |
parseRequest() | Parses the given request and returns the corresponding route and parameters. | GroupUrlRule |
createRules() protected | Creates the URL rules that should be contained within this composite rule. | GroupUrlRule |
iterateRules() protected | Iterates through specified rules and calls createUrl() for each of them. | CompositeUrlRule |
Property details
Section titled “Property details”$prefix
Section titled “$prefix ”public string $prefix = nullThe prefix for the pattern part of every rule declared in $rules.
The prefix and the pattern will be separated with a slash.
$routePrefix
Section titled “$routePrefix ”public string|null $routePrefix = nullThe prefix for the route part of every rule declared in $rules.
The prefix and the route will be separated with a slash.
If this property is not set, it will take the value of $prefix.
$ruleConfig
Section titled “$ruleConfig ”public array $ruleConfig = ['class' => 'yii\web\UrlRule']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
Section titled “$rules ”public yii\web\UrlRuleInterface[]|array[]|string[] $rules = []The rules contained within this composite rule.
Please refer to UrlManager::$rules
for the format of this property.
Method details
Section titled “Method details”createUrl()
Section titled “createUrl() ”public function createUrl(mixed $manager, mixed $route, mixed $params): string|boolCreates a URL according to the given route and parameters.
Parameters
| Parameter | Type | Description |
|---|---|---|
$manager | mixed | The URL manager |
$route | mixed | The route. It should not have slashes at the beginning or the end. |
$params | mixed | The parameters |
Returns
string|bool: The created URL, or false if this rule cannot be used for creating this URL.
init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
parseRequest()
Section titled “parseRequest() ”public function parseRequest(mixed $manager, mixed $request): array|boolParses the given request and returns the corresponding route and parameters.
Parameters
| Parameter | Type | Description |
|---|---|---|
$manager | mixed | The URL manager |
$request | mixed | 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.
createRules()
Section titled “createRules() ”protected function createRules(): yii\web\UrlRuleInterface[]|yii\web\UrlRuleInterface[][]Creates the URL rules that should be contained within this composite rule.
Returns
UrlRuleInterface[]|UrlRuleInterface[][]: The URL rules