UrlNormalizer
API reference › yii › web › UrlNormalizer
UrlNormalizer normalizes URLs for UrlManager and UrlRule.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$action | int| | Action to perform during route normalization. Default: self::ACTION_REDIRECT_PERMANENT. | UrlNormalizer |
$collapseSlashes | bool | Whether slashes should be collapsed, for example site///index will be converted into site/index Default: true. | UrlNormalizer |
$normalizeTrailingSlash | bool | Whether trailing slash should be normalized according to the suffix settings of the rule Default: true. | UrlNormalizer |
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\ |
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. | yii\ |
normalizePathInfo() | Normalizes specified pathInfo. | UrlNormalizer |
normalizeRoute() | Performs normalization action for the specified $route. | UrlNormalizer |
collapseSlashes() protected | Collapse consecutive slashes in $pathInfo, for example converts site///index into site/index. | UrlNormalizer |
normalizeTrailingSlash() protected | Adds or removes trailing slashes from $pathInfo depending on whether the $suffix has a trailing slash or not. | UrlNormalizer |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
ACTION_REDIRECT_PERMANENT | 301 | Represents permament redirection during route normalization. | UrlNormalizer |
ACTION_REDIRECT_TEMPORARY | 302 | Represents temporary redirection during route normalization. | UrlNormalizer |
ACTION_NOT_FOUND | 404 | Represents showing 404 error page during route normalization. | UrlNormalizer |
Property details
Section titled “Property details”$action
Section titled “$action ”public int|callable|null $action = self::ACTION_REDIRECT_PERMANENTAction to perform during route normalization.
Available options are:
-
null- no special action will be performed -
301- the request should be redirected to the normalized URL using permanent redirection -
302- the request should be redirected to the normalized URL using temporary redirection -
404-NotFoundHttpExceptionwill be thrown -
callable- custom user callback, for example:function ($route, $normalizer) {// use custom action for redirections$route[1]['oldRoute'] = $route[0];$route[0] = 'site/redirect';return $route;}
Method details
Section titled “Method details”normalizePathInfo()
Section titled “normalizePathInfo() ”public function normalizePathInfo(string $pathInfo, string $suffix, bool &$normalized = false): stringNormalizes specified pathInfo.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pathInfo | string | PathInfo for normalization |
$suffix | string | Current rule suffix |
$normalized | bool | If specified, this variable will be set to true if $pathInfo was changed during normalization |
Returns
string: Normalized pathInfo
normalizeRoute()
Section titled “normalizeRoute() ”public function normalizeRoute(array $route): arrayPerforms normalization action for the specified $route.
Parameters
| Parameter | Type | Description |
|---|---|---|
$route | array | Route for normalization |
Returns
array: Normalized route
Throws
yii\base\InvalidConfigException: if invalid normalization action is used.UrlNormalizerRedirectException: if normalization requires redirection.NotFoundHttpException: if normalization suggests action matching route does not exist.
collapseSlashes()
Section titled “collapseSlashes() ”protected function collapseSlashes(string $pathInfo): stringCollapse consecutive slashes in $pathInfo, for example converts site///index into site/index.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pathInfo | string | Raw path info. |
Returns
string: Normalized path info.
normalizeTrailingSlash()
Section titled “normalizeTrailingSlash() ”protected function normalizeTrailingSlash(string $pathInfo, string $suffix): stringAdds or removes trailing slashes from $pathInfo depending on whether the $suffix has a trailing slash or not.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pathInfo | string | Raw path info. |
$suffix | string |
Returns
string: Normalized path info.