Skip to content
PHP Ecosystem
Explore

UrlNormalizer

API reference › yii › web › UrlNormalizer

classyii\web\UrlNormalizer
Extendsyii\base\BaseObject
Implementsyii\base\Configurable
Since2.0.10
Sourceframework/web/UrlNormalizer.php

UrlNormalizer normalizes URLs for UrlManager and UrlRule.

PropertyTypeDescriptionDefined by
$actionint|callable|nullAction to perform during route normalization. Default: self::ACTION_REDIRECT_PERMANENT.UrlNormalizer
$collapseSlashesboolWhether slashes should be collapsed, for example site///index will be converted into site/index Default: true.UrlNormalizer
$normalizeTrailingSlashboolWhether trailing slash should be normalized according to the suffix settings of the rule Default: true.UrlNormalizer
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
normalizePathInfo()Normalizes specified pathInfo.UrlNormalizer
normalizeRoute()Performs normalization action for the specified $route.UrlNormalizer
collapseSlashes() protectedCollapse consecutive slashes in $pathInfo, for example converts site///index into site/index.UrlNormalizer
normalizeTrailingSlash() protectedAdds or removes trailing slashes from $pathInfo depending on whether the $suffix has a trailing slash or not.UrlNormalizer
ConstantValueDescriptionDefined by
ACTION_REDIRECT_PERMANENT301Represents permament redirection during route normalization.UrlNormalizer
ACTION_REDIRECT_TEMPORARY302Represents temporary redirection during route normalization.UrlNormalizer
ACTION_NOT_FOUND404Represents showing 404 error page during route normalization.UrlNormalizer
public int|callable|null $action = self::ACTION_REDIRECT_PERMANENT

Action 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 - NotFoundHttpException will 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;
    }
public function normalizePathInfo(string $pathInfo, string $suffix, bool &$normalized = false): string

Normalizes specified pathInfo.

Parameters

ParameterTypeDescription
$pathInfostringPathInfo for normalization
$suffixstringCurrent rule suffix
$normalizedboolIf specified, this variable will be set to true if $pathInfo was changed during normalization

Returns

string: Normalized pathInfo

public function normalizeRoute(array $route): array

Performs normalization action for the specified $route.

Parameters

ParameterTypeDescription
$routearrayRoute for normalization

Returns

array: Normalized route

Throws

protected function collapseSlashes(string $pathInfo): string

Collapse consecutive slashes in $pathInfo, for example converts site///index into site/index.

Parameters

ParameterTypeDescription
$pathInfostringRaw path info.

Returns

string: Normalized path info.

protected function normalizeTrailingSlash(string $pathInfo, string $suffix): string

Adds or removes trailing slashes from $pathInfo depending on whether the $suffix has a trailing slash or not.

Parameters

ParameterTypeDescription
$pathInfostringRaw path info.
$suffixstring

Returns

string: Normalized path info.