# UrlManager

> UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.

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

| class | `yii\web\UrlManager` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/UrlManager.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/UrlManager.php#L51) |

1 subclass:

- [`yii2\extensions\localeurls\UrlLanguageManager`](/docs/extensions/localeurls/reference/urllanguagemanager/index.md)

UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.

UrlManager is configured as an application component in [`Application`](/docs/api/yii/base/application/index.md) by default.
You can access that instance via `Yii::$app->urlManager`.

You can modify its configuration by adding an array to your application config under `components`
as it is shown in the following example:

```
'urlManager' => [
    'enablePrettyUrl' => true,
    'rules' => [
        // your rules go here
    ],
    // ...
]
```

Rules are classes implementing the [`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md), by default that is [`UrlRule`](/docs/api/yii/web/urlrule/index.md).
For nesting rules, there is also a [`GroupUrlRule`](/docs/api/yii/web/groupurlrule/index.md) class.

For more details and usage information on UrlManager, see the [guide article on routing](/docs/guide/runtime-routing/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-baseUrl"></span>`$baseUrl` | `string` | The base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. | `UrlManager` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$cache`](#property-cache) | [`yii\caching\CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md)`\|array\|string\|bool\|null` | The cache object or the application component ID of the cache object. Default: `'cache'`. | `UrlManager` |
| [`$enablePrettyUrl`](#property-enablePrettyUrl) | `bool` | Whether to enable pretty URLs. Default: `false`. | `UrlManager` |
| [`$enableStrictParsing`](#property-enableStrictParsing) | `bool` | Whether to enable strict parsing. Default: `false`. | `UrlManager` |
| <span id="property-hostInfo"></span>`$hostInfo` | `string` | The host info (e.g. `https://www.example.com`) that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs. | `UrlManager` |
| [`$normalizer`](#property-normalizer) | [`UrlNormalizer`](/docs/api/yii/web/urlnormalizer/index.md)`\|array\|string\|false` | The configuration for [`UrlNormalizer`](/docs/api/yii/web/urlnormalizer/index.md) used by this UrlManager. Default: `false`. Available since 2.0.10. | `UrlManager` |
| [`$routeParam`](#property-routeParam) | `string` | The GET parameter name for route. Default: `'r'`. | `UrlManager` |
| [`$ruleConfig`](#property-ruleConfig) | `array` | The default configuration of URL rules. Default: `['class' => 'yii\web\UrlRule']`. | `UrlManager` |
| [`$rules`](#property-rules) | `array` | The rules for creating and parsing URLs when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`. Default: `[]`. | `UrlManager` |
| <span id="property-scriptUrl"></span>`$scriptUrl` | `string` | The entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. | `UrlManager` |
| [`$showScriptName`](#property-showScriptName) | `bool` | Whether to show entry script name in the constructed URL. Default: `true`. | `UrlManager` |
| [`$suffix`](#property-suffix) | `string` | The URL suffix used when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`. Default: `null`. | `UrlManager` |
| <span id="property-cacheKey"></span>`$cacheKey` protected | `string` | The cache key for cached rules Default: `__CLASS__`. Available since 2.0.8. | `UrlManager` |

## 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/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`addRules()`](#method-addRules) | Adds additional URL rules. | `UrlManager` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/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) |
| [`createAbsoluteUrl()`](#method-createAbsoluteUrl) | Creates an absolute URL using the given route and query parameters. | `UrlManager` |
| [`createUrl()`](#method-createUrl) | Creates a URL using the given route and query parameters. | `UrlManager` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getBaseUrl()`](#method-getBaseUrl) | Returns the base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. | `UrlManager` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getHostInfo()`](#method-getHostInfo) | Returns the host info that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs. | `UrlManager` |
| [`getScriptUrl()`](#method-getScriptUrl) | Returns the entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. | `UrlManager` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes UrlManager. | `UrlManager` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`parseRequest()`](#method-parseRequest) | Parses the user request. | `UrlManager` |
| [`setBaseUrl()`](#method-setBaseUrl) | Sets the base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. | `UrlManager` |
| [`setHostInfo()`](#method-setHostInfo) | Sets the host info that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs. | `UrlManager` |
| [`setScriptUrl()`](#method-setScriptUrl) | Sets the entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. | `UrlManager` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`buildRules()`](#method-buildRules) protected | Builds URL rule objects from the given rule declarations. | `UrlManager` |
| [`canBeCached()`](#method-canBeCached) protected | Returns the value indicating whether result of [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) of rule should be cached in internal cache. | `UrlManager` |
| [`getBuiltRulesFromCache()`](#method-getBuiltRulesFromCache) protected | Provides the built URL rules that are associated with the $ruleDeclarations from cache. | `UrlManager` |
| [`getUrlFromCache()`](#method-getUrlFromCache) protected | Get URL from internal cache if exists. | `UrlManager` |
| [`setBuiltRulesCache()`](#method-setBuiltRulesCache) protected | Stores $builtRules to cache, using $rulesDeclaration as a part of cache key. | `UrlManager` |
| [`setRuleToCache()`](#method-setRuleToCache) protected | Store rule (e.g. [`UrlRule`](/docs/api/yii/web/urlrule/index.md)) to internal cache. | `UrlManager` |

## Property details

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

```php
public yii\caching\CacheInterface|array|string|bool|null $cache = 'cache'
```

The cache object or the application component ID of the cache object.

This can also be an array that is used to create a [`CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md) instance in case you do not want to use
an application component.
Compiled URL rules will be cached through this cache object, if it is available.

After the UrlManager object is created, if you want to change this property,
you should only assign it with a cache object.
Set this property to `false` or `null` if you do not want to cache the URL rules.

Cache entries are stored for the time set by [$defaultDuration](/docs/api/yii/caching/cache/index.md#property-defaultDuration) in
the cache configuration, which is unlimited by default. You may want to tune this value if your [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules)
change frequently.

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

```php
public bool $enablePrettyUrl = false
```

Whether to enable pretty URLs.

Instead of putting all parameters in the query
string part of a URL, pretty URLs allow using path info to represent some of the parameters
and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of
"/index.php?r=news%2Fview&id=100".

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

```php
public bool $enableStrictParsing = false
```

Whether to enable strict parsing.

If strict parsing is enabled, the incoming
requested URL must match at least one of the [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules) in order to be treated as a valid request.
Otherwise, the path info part of the request will be treated as the requested route.
This property is used only when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`.

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

```php
public yii\web\UrlNormalizer|array|string|false $normalizer = false
```

The configuration for [`UrlNormalizer`](/docs/api/yii/web/urlnormalizer/index.md) used by this UrlManager.

The default value is `false`, which means normalization will be skipped.
If you wish to enable URL normalization, you should configure this property manually.
For example:

```
[
    'class' => 'yii\web\UrlNormalizer',
    'collapseSlashes' => true,
    'normalizeTrailingSlash' => true,
]
```

**Since**

2.0.10

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

```php
public string $routeParam = 'r'
```

The GET parameter name for route.

This property is used only if [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `false`.

### $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/urlmanager/index.md#property-rules) will take precedence when the same property of the rule is configured.

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

```php
public array $rules = []
```

The rules for creating and parsing URLs when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`.

This property is used only if [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`. Each element in the array
is the configuration array for creating a single URL rule. The configuration will
be merged with [`$ruleConfig`](/docs/api/yii/web/urlmanager/index.md#property-ruleConfig) first before it is used for creating the rule object.

A special shortcut format can be used if a rule only specifies [pattern](/docs/api/yii/web/urlrule/index.md#property-pattern)
and [route](/docs/api/yii/web/urlrule/index.md#property-route): `'pattern' => 'route'`. That is, instead of using a configuration
array, one can use the key to represent the pattern and the value the corresponding route.
For example, `'post/<id:\d+>' => 'post/view'`.

For RESTful routing the mentioned shortcut format also allows you to specify the
[HTTP verb](/docs/api/yii/web/urlrule/index.md#property-verb) that the rule should apply for.
You can do that  by prepending it to the pattern, separated by space.
For example, `'PUT post/<id:\d+>' => 'post/update'`.
You may specify multiple verbs by separating them with comma
like this: `'POST,PUT post/index' => 'post/create'`.
The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE.
Note that [mode](/docs/api/yii/web/urlrule/index.md#property-mode) will be set to PARSING_ONLY when specifying verb in this way
so you normally would not specify a verb for normal GET request.

Here is an example configuration for RESTful CRUD controller:

```
[
    'dashboard' => 'site/index',

    'POST <controller:[\w-]+>' => '<controller>/create',
    '<controller:[\w-]+>s' => '<controller>/index',

    'PUT <controller:[\w-]+>/<id:\d+>'    => '<controller>/update',
    'DELETE <controller:[\w-]+>/<id:\d+>' => '<controller>/delete',
    '<controller:[\w-]+>/<id:\d+>'        => '<controller>/view',
];
```

Note that if you modify this property after the UrlManager object is created, make sure
you populate the array with rule objects instead of rule configurations.

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

```php
public bool $showScriptName = true
```

Whether to show entry script name in the constructed URL.

Defaults to `true`.
This property is used only if [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`.

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

```php
public string $suffix = null
```

The URL suffix used when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`.

For example, ".html" can be used so that the URL looks like pointing to a static HTML page.
This property is used only if [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true`.

## Method details

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

```php
public function addRules(array $rules, bool $append = true): mixed
```

Adds additional URL rules.

This method will call [`buildRules()`](/docs/api/yii/web/urlmanager/index.md#method-buildRules) to parse the given rule declarations and then append or insert
them to the existing [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules).

Note that if [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `false`, this method will do nothing.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rules` | `array` | The new rules to be added. Each array element represents a single rule declaration. Please refer to [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules) for the acceptable rule format. |
| `$append` | `bool` | Whether to add the new rules by appending them to the end of the existing rules. |

**Returns**

`mixed`

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

```php
public function createAbsoluteUrl(string|array $params, string|null $scheme = null): string
```

Creates an absolute URL using the given route and query parameters.

This method prepends the URL created by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) with the [`$hostInfo`](/docs/api/yii/web/urlmanager/index.md#property-hostInfo).

Note that unlike [`Url::toRoute()`](/docs/api/yii/helpers/baseurl/index.md#method-toRoute), this method always treats the given route
as an absolute route.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$params` | `string\|array` | Use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |
| `$scheme` | `string\|null` | The scheme to use for the URL (either `http`, `https` or empty string for protocol-relative URL). If not specified the scheme of the current request will be used. |

**Returns**

`string`: The created URL

**See also**

- [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl)

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

```php
public function createUrl(string|array $params): string
```

Creates a URL using the given route and query parameters.

You may specify the route as a string, e.g., `site/index`. You may also use an array
if you want to specify additional query parameters for the URL being created. The
array format must be:

```
// generates: /index.php?r=site%2Findex&param1=value1&param2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']
```

If you want to create a URL with an anchor, you can use the array format with a `#` parameter.
For example,

```
// generates: /index.php?r=site%2Findex&param1=value1#name
['site/index', 'param1' => 'value1', '#' => 'name']
```

The URL created is a relative one. Use [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to create an absolute URL.

Note that unlike [`Url::toRoute()`](/docs/api/yii/helpers/baseurl/index.md#method-toRoute), this method always treats the given route
as an absolute route.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$params` | `string\|array` | Use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`). |

**Returns**

`string`: The created URL

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

```php
public function getBaseUrl(): string
```

Returns the base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs.

It defaults to [`Request::$baseUrl`](/docs/api/yii/web/request/index.md#property-baseUrl).
This is mainly used when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true` and [`$showScriptName`](/docs/api/yii/web/urlmanager/index.md#property-showScriptName) is `false`.

**Returns**

`string`: The base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if running in console application and [`$baseUrl`](/docs/api/yii/web/urlmanager/index.md#property-baseUrl) is not configured.

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

```php
public function getHostInfo(): string
```

Returns the host info that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs.

**Returns**

`string`: The host info (e.g. `https://www.example.com`) that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if running in console application and [`$hostInfo`](/docs/api/yii/web/urlmanager/index.md#property-hostInfo) is not configured.

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

```php
public function getScriptUrl(): string
```

Returns the entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs.

It defaults to [`Request::$scriptUrl`](/docs/api/yii/web/request/index.md#property-scriptUrl).
This is mainly used when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `false` or [`$showScriptName`](/docs/api/yii/web/urlmanager/index.md#property-showScriptName) is `true`.

**Returns**

`string`: The entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if running in console application and [`$scriptUrl`](/docs/api/yii/web/urlmanager/index.md#property-scriptUrl) is not configured.

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

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

Initializes UrlManager.

**Returns**

`mixed`

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

```php
public function parseRequest(yii\web\Request $request): array|bool
```

Parses the user request.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$request` | [`Request`](/docs/api/yii/web/request/index.md) | The request component |

**Returns**

`array|bool`: The route and the associated parameters. The latter is always empty if [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `false`. `false` is returned if the current request cannot be successfully parsed.

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

```php
public function setBaseUrl(string $value): mixed
```

Sets the base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs.

This is mainly used when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `true` and [`$showScriptName`](/docs/api/yii/web/urlmanager/index.md#property-showScriptName) is `false`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | The base URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. |

**Returns**

`mixed`

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

```php
public function setHostInfo(string $value): mixed
```

Sets the host info that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | The host info (e.g. "https://www.example.com") that is used by [`createAbsoluteUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createAbsoluteUrl) to prepend to created URLs. |

**Returns**

`mixed`

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

```php
public function setScriptUrl(string $value): mixed
```

Sets the entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs.

This is mainly used when [`$enablePrettyUrl`](/docs/api/yii/web/urlmanager/index.md#property-enablePrettyUrl) is `false` or [`$showScriptName`](/docs/api/yii/web/urlmanager/index.md#property-showScriptName) is `true`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | The entry script URL that is used by [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) to prepend to created URLs. |

**Returns**

`mixed`

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

```php
protected function buildRules(array $ruleDeclarations): yii\web\UrlRuleInterface[]
```

Builds URL rule objects from the given rule declarations.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$ruleDeclarations` | `array` | The rule declarations. Each array element represents a single rule declaration. Please refer to [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules) for the acceptable rule formats. |

**Returns**

[`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md)`[]`: The rule objects built from the given rule declarations

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a rule declaration is invalid

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

```php
protected function canBeCached(yii\web\UrlRuleInterface $rule): bool
```

Returns the value indicating whether result of [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl) of rule should be cached in internal cache.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rule` | [`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md) |  |

**Returns**

`bool`: `true` if result should be cached, `false` if not.

**Since**

2.0.12

**See also**

- [`getUrlFromCache()`](/docs/api/yii/web/urlmanager/index.md#method-getUrlFromCache)
- [`setRuleToCache()`](/docs/api/yii/web/urlmanager/index.md#method-setRuleToCache)
- [`UrlRule::getCreateUrlStatus()`](/docs/api/yii/web/urlrule/index.md#method-getCreateUrlStatus)

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

```php
protected function getBuiltRulesFromCache(array $ruleDeclarations): yii\web\UrlRuleInterface[]|false
```

Provides the built URL rules that are associated with the $ruleDeclarations from cache.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$ruleDeclarations` | `array` | The rule declarations. Each array element represents a single rule declaration. Please refer to [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules) for the acceptable rule formats. |

**Returns**

[`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md)`[]|false`: The rule objects built from the given rule declarations or boolean `false` when there are no cache items for this definition exists.

**Since**

2.0.14

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

```php
protected function getUrlFromCache(string $cacheKey, string $route, array $params): bool|string
```

Get URL from internal cache if exists.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cacheKey` | `string` | Generated cache key to store data. |
| `$route` | `string` | The route (e.g. `site/index`). |
| `$params` | `array` | Rule params. |

**Returns**

`bool|string`: The created URL

**Since**

2.0.8

**See also**

- [`createUrl()`](/docs/api/yii/web/urlmanager/index.md#method-createUrl)

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

```php
protected function setBuiltRulesCache(array $ruleDeclarations, yii\web\UrlRuleInterface[] $builtRules): bool
```

Stores $builtRules to cache, using $rulesDeclaration as a part of cache key.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$ruleDeclarations` | `array` | The rule declarations. Each array element represents a single rule declaration. Please refer to [`$rules`](/docs/api/yii/web/urlmanager/index.md#property-rules) for the acceptable rule formats. |
| `$builtRules` | [`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md)`[]` | The rule objects built from the given rule declarations. |

**Returns**

`bool`: Whether the value is successfully stored into cache

**Since**

2.0.14

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

```php
protected function setRuleToCache(mixed $cacheKey, yii\web\UrlRuleInterface $rule): mixed
```

Store rule (e.g. [`UrlRule`](/docs/api/yii/web/urlrule/index.md)) to internal cache.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cacheKey` | `mixed` |  |
| `$rule` | [`UrlRuleInterface`](/docs/api/yii/web/urlruleinterface/index.md) |  |

**Returns**

`mixed`

**Since**

2.0.8