# GroupUrlRule

> GroupUrlRule represents a collection of URL rules sharing the same prefix in their patterns and routes.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › GroupUrlRule

| class | `yii\web\GroupUrlRule` |
| --- | --- |
| Extends | [`CompositeUrlRule`](/docs/api/yii/web/compositeurlrule/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/GroupUrlRule.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/GroupUrlRule.php#L49) |

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`](/docs/api/yii/web/groupurlrule/index.md#property-prefix) and [`$routePrefix`](/docs/api/yii/web/groupurlrule/index.md#property-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

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-createUrlStatus"></span>[`$createUrlStatus`](/docs/api/yii/web/compositeurlrule/index.md#property-createUrlStatus) read-only | `int\|null` | Status of the URL creation after the last [`createUrl()`](/docs/api/yii/web/compositeurlrule/index.md#method-createUrl) call. `null` if rule does not provide info about create status. | [`CompositeUrlRule`](/docs/api/yii/web/compositeurlrule/index.md) |
| [`$prefix`](#property-prefix) | `string` | The prefix for the pattern part of every rule declared in [`$rules`](/docs/api/yii/web/groupurlrule/index.md#property-rules). Default: `null`. | `GroupUrlRule` |
| [`$routePrefix`](#property-routePrefix) | `string\|null` | The prefix for the route part of every rule declared in [`$rules`](/docs/api/yii/web/groupurlrule/index.md#property-rules). Default: `null`. | `GroupUrlRule` |
| [`$ruleConfig`](#property-ruleConfig) | `array` | The default configuration of URL rules. Default: `['class' => 'yii\web\UrlRule']`. | `GroupUrlRule` |
| [`$rules`](#property-rules) | [`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md)`[]\|array[]\|string[]` | The rules contained within this composite rule. Default: `[]`. | `GroupUrlRule` |
| <span id="property-createStatus"></span>[`$createStatus`](/docs/api/yii/web/compositeurlrule/index.md#property-createStatus) protected | `int\|null` | Status of the URL creation after the last [`createUrl()`](/docs/api/yii/web/compositeurlrule/index.md#method-createUrl) call. Default: `null`. Available since 2.0.12. | [`CompositeUrlRule`](/docs/api/yii/web/compositeurlrule/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`createUrl()`](#method-createUrl) | Creates a URL according to the given route and parameters. | `GroupUrlRule` |
| <span id="method-getCreateUrlStatus"></span>[`getCreateUrlStatus()`](/docs/api/yii/web/compositeurlrule/index.md#method-getCreateUrlStatus) | Returns status of the URL creation after the last [`createUrl()`](/docs/api/yii/web/compositeurlrule/index.md#method-createUrl) call. | [`CompositeUrlRule`](/docs/api/yii/web/compositeurlrule/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`init()`](#method-init) | Initializes the object. | `GroupUrlRule` |
| [`parseRequest()`](#method-parseRequest) | Parses the given request and returns the corresponding route and parameters. | `GroupUrlRule` |
| [`createRules()`](#method-createRules) protected | Creates the URL rules that should be contained within this composite rule. | `GroupUrlRule` |
| <span id="method-iterateRules"></span>[`iterateRules()`](/docs/api/yii/web/compositeurlrule/index.md#method-iterateRules) protected | Iterates through specified rules and calls [`createUrl()`](/docs/api/yii/web/compositeurlrule/index.md#method-createUrl) for each of them. | [`CompositeUrlRule`](/docs/api/yii/web/compositeurlrule/index.md) |

## Property details

### $prefix <span id="property-prefix"></span>

```php
public string $prefix = null
```

The prefix for the pattern part of every rule declared in [`$rules`](/docs/api/yii/web/groupurlrule/index.md#property-rules).

The prefix and the pattern will be separated with a slash.

### $routePrefix <span id="property-routePrefix"></span>

```php
public string|null $routePrefix = null
```

The prefix for the route part of every rule declared in [`$rules`](/docs/api/yii/web/groupurlrule/index.md#property-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`](/docs/api/yii/web/groupurlrule/index.md#property-prefix).

### $ruleConfig <span id="property-ruleConfig"></span>

```php
public array $ruleConfig = ['class' => 'yii\web\UrlRule']
```

The default configuration of URL rules.

Individual rule configurations
specified via [`$rules`](/docs/api/yii/web/groupurlrule/index.md#property-rules) will take precedence when the same property of the rule is configured.

### $rules <span id="property-rules"></span>

```php
public yii\web\UrlRuleInterface[]|array[]|string[] $rules = []
```

The rules contained within this composite rule.

Please refer to [`UrlManager::$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules)
for the format of this property.

## Method details

### createUrl() <span id="method-createUrl"></span>

```php
public function createUrl(mixed $manager, mixed $route, mixed $params): string|bool
```

Creates 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() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### parseRequest() <span id="method-parseRequest"></span>

```php
public function parseRequest(mixed $manager, mixed $request): array|bool
```

Parses 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() <span id="method-createRules"></span>

```php
protected function createRules(): yii\web\UrlRuleInterface[]|yii\web\UrlRuleInterface[][]
```

Creates the URL rules that should be contained within this composite rule.

**Returns**

[`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md)`[]|`[`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md)`[][]`: The URL rules