类 yii\web\UrlNormalizer
| 继承关系 | yii\web\UrlNormalizer » yii\base\BaseObject |
|---|---|
| 实现接口 | yii\base\Configurable |
| 可用版本 | 2.0.10 |
| 源代码 | https://github.com/yiisoft/yii2/blob/master/framework/web/UrlNormalizer.php |
UrlNormalizer 规范化 yii\web\UrlManager 和 yii\web\UrlRule 的 URL。
公共属性
| 属性 | 类型 | 描述 | 定义于 |
|---|---|---|---|
| $action | integer|callable|null | 在路由规范化期间执行的操作。 | yii\web\UrlNormalizer |
| $collapseSlashes | boolean | 是否应该折叠斜杠,例如 site///index 将转换为 site/index |
yii\web\UrlNormalizer |
| $normalizeTrailingSlash | boolean | 是否应根据规则的后缀设置规范化尾部斜杠 | yii\web\UrlNormalizer |
公共方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| __call() | 调用不是类方法的命名方法。 | yii\base\BaseObject |
| __construct() | 构造函数。 | yii\base\BaseObject |
| __get() | 返回对象属性的值。 | yii\base\BaseObject |
| __isset() | 检查属性是否已设置,即定义且不为 null。 | yii\base\BaseObject |
| __set() | 设置对象属性的值。 | yii\base\BaseObject |
| __unset() | 将对象属性设置为 null。 | yii\base\BaseObject |
| canGetProperty() | 返回一个值,指示是否可以读取属性。 | yii\base\BaseObject |
| canSetProperty() | 返回一个值,指示是否可以设置属性。 | yii\base\BaseObject |
| className() | 返回此类的完全限定名称。 | yii\base\BaseObject |
| hasMethod() | 返回一个值,指示方法是否已定义。 | yii\base\BaseObject |
| hasProperty() | 返回一个值,指示属性是否已定义。 | yii\base\BaseObject |
| init() | 初始化对象。 | yii\base\BaseObject |
| normalizePathInfo() | 规范化指定的 pathInfo。 | yii\web\UrlNormalizer |
| normalizeRoute() | 对指定的 $route 执行规范化操作。 | yii\web\UrlNormalizer |
受保护的方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| collapseSlashes() | 折叠 $pathInfo 中连续的斜杠,例如将 site///index 转换为 site/index。 |
yii\web\UrlNormalizer |
| normalizeTrailingSlash() | 根据 $suffix 是否有尾部斜杠,从 $pathInfo 中添加或删除尾部斜杠。 | yii\web\UrlNormalizer |
常量
| 常量 | 值 | 描述 | 定义于 |
|---|---|---|---|
| ACTION_NOT_FOUND | 404 | 表示在路由规范化期间显示 404 错误页面。 | yii\web\UrlNormalizer |
| ACTION_REDIRECT_PERMANENT | 301 | 表示在路由规范化期间永久重定向。 | yii\web\UrlNormalizer |
| ACTION_REDIRECT_TEMPORARY | 302 | 表示在路由规范化期间临时重定向。 | yii\web\UrlNormalizer |
属性详情
在路由规范化期间执行的操作。可用的选项为
null- 不会执行任何特殊操作301- 请求应使用永久重定向重定向到规范化的 URL302- 请求应使用临时重定向重定向到规范化的 URL404- 将抛出 yii\web\NotFoundHttpExceptioncallable- 自定义用户回调,例如function ($route, $normalizer) { // use custom action for redirections $route[1]['oldRoute'] = $route[0]; $route[0] = 'site/redirect'; return $route; }
是否应该折叠斜杠,例如 site///index 将转换为 site/index
方法详情
| public mixed __call ( $name, $params ) | ||
| $name | string |
方法名 |
| $params | array |
方法参数 |
| 返回值 | mixed |
方法返回值 |
|---|---|---|
| 抛出异常 | yii\base\UnknownMethodException |
调用未知方法时 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定义于: yii\base\BaseObject::__construct()
构造函数。
默认实现执行两件事
- 使用给定的配置
$config初始化对象。 - 调用 init()。
如果此方法在子类中被重写,建议
- 构造函数的最后一个参数为配置数组,如这里的
$config。 - 在构造函数的末尾调用父类的实现。
| public void __construct ( $config = [] ) | ||
| $config | array |
用于初始化对象属性的键值对 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定义于: yii\base\BaseObject::__get()
返回对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 $value = $object->property; 时会被隐式调用。
另请参阅 __set()。
| public mixed __get ( $name ) | ||
| $name | string |
属性名称 |
| 返回值 | mixed |
属性值 |
|---|---|---|
| 抛出异常 | yii\base\UnknownPropertyException |
如果属性未定义 |
| 抛出异常 | yii\base\InvalidCallException |
如果属性为只写 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定义于: yii\base\BaseObject::__isset()
检查属性是否已设置,即定义且不为 null。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 isset($object->property) 时会被隐式调用。
请注意,如果属性未定义,则会返回 false。
| public boolean __isset ( $name ) | ||
| $name | string |
属性名称或事件名称 |
| 返回值 | boolean |
命名的属性是否已设置(非 null)。 |
|---|---|---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定义于: yii\base\BaseObject::__set()
设置对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 $object->property = $value; 时会被隐式调用。
另请参阅 __get()。
| public void __set ( $name, $value ) | ||
| $name | string |
属性名称或事件名称 |
| $value | mixed |
属性值 |
| 抛出异常 | yii\base\UnknownPropertyException |
如果属性未定义 |
|---|---|---|
| 抛出异常 | yii\base\InvalidCallException |
如果属性为只读 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
定义于: yii\base\BaseObject::__unset()
将对象属性设置为 null。
不要直接调用此方法,因为它是一个 PHP 魔术方法,在执行 unset($object->property) 时会被隐式调用。
请注意,如果属性未定义,此方法将不执行任何操作。如果属性为只读,则会抛出异常。
| public void __unset ( $name ) | ||
| $name | string |
属性名称 |
| 抛出异常 | yii\base\InvalidCallException |
如果属性为只读。 |
|---|---|---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
定义于: yii\base\BaseObject::canGetProperty()
返回一个值,指示是否可以读取属性。
如果属性可读,则
- 类具有与指定名称关联的 getter 方法(在这种情况下,属性名称不区分大小写);
- 类具有与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参阅 canSetProperty()。
| public boolean canGetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
属性是否可读 |
|---|---|---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定义于: yii\base\BaseObject::canSetProperty()
返回一个值,指示是否可以设置属性。
如果属性可写,则
- 类具有与指定名称关联的 setter 方法(在这种情况下,属性名称不区分大小写);
- 类具有与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参阅 canGetProperty()。
| public boolean canSetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
属性是否可写 |
|---|---|---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class。
定义于: yii\base\BaseObject::className()
返回此类的完全限定名称。
| public static string className ( ) | ||
| 返回值 | string |
此类的完整限定名称。 |
|---|---|---|
public static function className()
{
return get_called_class();
}
折叠 $pathInfo 中连续的斜杠,例如将 site///index 转换为 site/index。
| protected string collapseSlashes ( $pathInfo ) | ||
| $pathInfo | string |
原始路径信息。 |
| 返回值 | string |
规范化的路径信息。 |
|---|---|---|
protected function collapseSlashes($pathInfo)
{
return ltrim(preg_replace('#/{2,}#', '/', $pathInfo), '/');
}
定义于: yii\base\BaseObject::hasMethod()
返回一个值,指示方法是否已定义。
默认实现是对 php 函数 method_exists() 的调用。当您实现 php 魔术方法 __call() 时,可以重写此方法。
| public boolean hasMethod ( $name ) | ||
| $name | string |
方法名 |
| 返回值 | boolean |
方法是否已定义 |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定义于: yii\base\BaseObject::hasProperty()
返回一个值,指示属性是否已定义。
如果属性已定义,则
- 类具有与指定名称关联的 getter 或 setter 方法(在这种情况下,属性名称不区分大小写);
- 类具有与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参阅
| public boolean hasProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
属性是否已定义 |
|---|---|---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
| public void init ( ) |
public function init()
{
}
规范化指定的 pathInfo。
| public string normalizePathInfo ( $pathInfo, $suffix, &$normalized = false ) | ||
| $pathInfo | string |
用于标准化的 PathInfo |
| $suffix | string |
当前规则的后缀 |
| $normalized | boolean |
如果指定,则在标准化过程中如果 $pathInfo 发生更改,此变量将设置为 |
| 返回值 | string |
标准化的 PathInfo |
|---|---|---|
public function normalizePathInfo($pathInfo, $suffix, &$normalized = false)
{
if (empty($pathInfo)) {
return $pathInfo;
}
$sourcePathInfo = $pathInfo;
if ($this->collapseSlashes) {
$pathInfo = $this->collapseSlashes($pathInfo);
}
if ($this->normalizeTrailingSlash === true) {
$pathInfo = $this->normalizeTrailingSlash($pathInfo, $suffix);
}
$normalized = $sourcePathInfo !== $pathInfo;
return $pathInfo;
}
对指定的 $route 执行规范化操作。
| public array normalizeRoute ( $route ) | ||
| $route | array |
用于标准化的路由 |
| 返回值 | array |
标准化的路由 |
|---|---|---|
| 抛出异常 | yii\base\InvalidConfigException |
如果使用了无效的标准化操作。 |
| 抛出异常 | yii\web\UrlNormalizerRedirectException |
如果标准化需要重定向。 |
| 抛出异常 | yii\web\NotFoundHttpException |
如果标准化建议与路由匹配的操作不存在。 |
public function normalizeRoute($route)
{
if ($this->action === null) {
return $route;
} elseif ($this->action === static::ACTION_REDIRECT_PERMANENT || $this->action === static::ACTION_REDIRECT_TEMPORARY) {
throw new UrlNormalizerRedirectException([$route[0]] + $route[1], $this->action);
} elseif ($this->action === static::ACTION_NOT_FOUND) {
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
} elseif (is_callable($this->action)) {
return call_user_func($this->action, $route, $this);
}
throw new InvalidConfigException('Invalid normalizer action.');
}
根据 $suffix 是否有尾部斜杠,从 $pathInfo 中添加或删除尾部斜杠。
| protected string normalizeTrailingSlash ( $pathInfo, $suffix ) | ||
| $pathInfo | string |
原始路径信息。 |
| $suffix | string | |
| 返回值 | string |
规范化的路径信息。 |
|---|---|---|
protected function normalizeTrailingSlash($pathInfo, $suffix)
{
if (substr($suffix, -1) === '/' && substr($pathInfo, -1) !== '/') {
$pathInfo .= '/';
} elseif (substr($suffix, -1) !== '/' && substr($pathInfo, -1) === '/') {
$pathInfo = rtrim($pathInfo, '/');
}
return $pathInfo;
}
请注册或登录以发表评论。