0 追蹤者

類別 yii\web\GroupUrlRule

繼承關係yii\web\GroupUrlRule » yii\web\CompositeUrlRule » yii\base\BaseObject
實作介面yii\base\Configurable, yii\web\UrlRuleInterface
自版本2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/web/GroupUrlRule.php

GroupUrlRule 代表 URL 規則的集合,這些規則在其模式和路由中共享相同的前綴。

GroupUrlRule 最適合由模組使用,該模組經常使用模組 ID 作為 URL 規則的前綴。例如,以下程式碼為 admin 模組建立規則

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',
]

以上範例假設模式和路由的前綴相同。它們可以透過分別設定 $prefix$routePrefix 來使其不同。

使用 GroupUrlRule 比直接宣告其包含的個別規則更有效率。這是因為 GroupUrlRule 可以透過簡單地檢查前綴是否匹配,來快速判斷是否應處理 URL 解析或建立請求。

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$createStatus integer|null 上次 createUrl() 呼叫後 URL 建立的狀態。 yii\web\CompositeUrlRule
$createUrlStatus integer|null 上次 createUrl() 呼叫後 URL 建立的狀態。 yii\web\CompositeUrlRule
$prefix string $rules 中宣告的每個規則的模式部分的前綴。 yii\web\GroupUrlRule
$routePrefix string|null $rules 中宣告的每個規則的路由部分的前綴。 yii\web\GroupUrlRule
$ruleConfig array URL 規則的預設配置。 yii\web\GroupUrlRule
$rules array 此複合規則中包含的規則。 yii\web\GroupUrlRule

受保護的屬性

隱藏繼承的屬性

屬性 類型 描述 定義於

公開方法

隱藏繼承的方法

方法 描述 定義於
__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
createUrl() 根據給定的路由和參數建立 URL。 yii\web\GroupUrlRule
getCreateUrlStatus() 傳回上次 createUrl() 呼叫後 URL 建立的狀態。 yii\web\CompositeUrlRule
hasMethod() 傳回指示是否已定義方法的值。 yii\base\BaseObject
hasProperty() 傳回指示是否已定義屬性的值。 yii\base\BaseObject
init() 初始化物件。 yii\web\GroupUrlRule
parseRequest() 解析給定的請求,並傳回對應的路由和參數。 yii\web\GroupUrlRule

受保護的方法

隱藏繼承的方法

方法 描述 定義於
createRules() 建立應包含在此複合規則中的 URL 規則。 yii\web\GroupUrlRule
iterateRules() 迭代指定的規則,並為每個規則呼叫 createUrl() yii\web\CompositeUrlRule

屬性詳細資訊

隱藏繼承的屬性

$prefix 公開屬性

$rules 中宣告的每個規則的 pattern 部分的前綴。前綴和 pattern 將以斜線分隔。

public string $prefix null
$routePrefix public property

$rules 中宣告的每個規則的 route 部分的前綴。前綴和 route 將以斜線分隔。如果未設定此屬性,它將採用 $prefix 的值。

public string|null $routePrefix null
$ruleConfig public property

URL 規則的預設配置。透過 $rules 指定的個別規則配置,在配置規則的相同屬性時,將優先採用。

public array $ruleConfig = [
    
'class' => 'yii\web\UrlRule',
]
$rules public property

此複合規則中包含的規則。有關此屬性的格式,請參閱 yii\web\UrlManager::$rules

另請參閱

public array $rules = []

方法詳細資訊

隱藏繼承的方法

__call() public method

定義於: yii\base\BaseObject::__call()

呼叫未作為類別方法的具名方法。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當呼叫未知方法時,它會被隱式呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

定義於: 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();
}

            
__get() public method

定義於: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property; 時,它會被隱式呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws 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);
}

            
__isset() public method

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property) 時,它會被隱式呼叫。

請注意,如果屬性未定義,將傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

具名屬性是否已設定(非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果屬性未定義

throws 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);
    }
}

            
__unset() public method

定義於: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property) 時,它會被隱式呼叫。

請注意,如果屬性未定義,此方法將不執行任何操作。如果屬性為唯讀,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws 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);
    }
}

            
canGetProperty() public method

定義於: yii\base\BaseObject::canGetProperty()

傳回指示屬性是否可讀取的值。

如果符合以下條件,屬性是可讀的

  • 類別具有與指定名稱相關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可讀

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

定義於: yii\base\BaseObject::canSetProperty()

傳回指示屬性是否可設定的值。

如果符合以下條件,屬性是可寫入的

  • 類別具有與指定名稱相關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可寫入

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
自 2.0.14 版本起已過時。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
createRules() protected method

建立應包含在此複合規則中的 URL 規則。

protected yii\web\UrlRuleInterface[] createRules ( )
return yii\web\UrlRuleInterface[]

URL 規則

                protected function createRules()
{
    $rules = [];
    foreach ($this->rules as $key => $rule) {
        if (!is_array($rule)) {
            $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
            $verb = null;
            if (preg_match("/^((?:(?:$verbs),)*(?:$verbs))\\s+(.*)$/", $key, $matches)) {
                $verb = explode(',', $matches[1]);
                $key = $matches[2];
            }
            $rule = [
                'pattern' => ltrim($this->prefix . '/' . $key, '/'),
                'route' => ltrim($this->routePrefix . '/' . $rule, '/'),
                'verb' => $verb
            ];
        } elseif (isset($rule['pattern'], $rule['route'])) {
            $rule['pattern'] = ltrim($this->prefix . '/' . $rule['pattern'], '/');
            $rule['route'] = ltrim($this->routePrefix . '/' . $rule['route'], '/');
        }
        $rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
        if (!$rule instanceof UrlRuleInterface) {
            throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.');
        }
        $rules[] = $rule;
    }
    return $rules;
}

            
createUrl() public method

根據給定的路由和參數建立 URL。

public string|boolean createUrl ( $manager, $route, $params )
$manager yii\web\UrlManager

URL 管理器

$route string

路由。開頭或結尾不應有斜線。

$params array

參數

return string|boolean

建立的 URL,如果此規則不能用於建立此 URL,則為 false。

                public function createUrl($manager, $route, $params)
{
    if ($this->routePrefix === '' || strpos($route, $this->routePrefix . '/') === 0) {
        return parent::createUrl($manager, $route, $params);
    }
    $this->createStatus = UrlRule::CREATE_STATUS_ROUTE_MISMATCH;
    return false;
}

            
getCreateUrlStatus() public method (available since version 2.0.12)

定義於: yii\web\CompositeUrlRule::getCreateUrlStatus()

傳回上次 createUrl() 呼叫後 URL 建立的狀態。

對於多個規則,狀態將通過位元運算子 or 組合(例如 UrlRule::CREATE_STATUS_PARSING_ONLY | UrlRule::CREATE_STATUS_PARAMS_MISMATCH)。

另請參閱

public integer|null getCreateUrlStatus ( )
return integer|null

上次 createUrl() 呼叫後 URL 建立的狀態。如果規則未提供有關建立狀態的資訊,則為 null

                public function getCreateUrlStatus()
{
    return $this->createStatus;
}

            
hasMethod() public method

定義於: yii\base\BaseObject::hasMethod()

傳回指示是否已定義方法的值。

預設實作是呼叫 php 函數 method_exists()。當您實作了 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

定義於: yii\base\BaseObject::hasProperty()

傳回指示是否已定義屬性的值。

如果符合以下條件,則定義了屬性

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

初始化物件。

在物件使用給定的配置初始化後,此方法會在建構子的結尾被調用。

public void init ( )

                public function init()
{
    $this->prefix = trim((string)$this->prefix, '/');
    $this->routePrefix = $this->routePrefix === null ? $this->prefix : trim($this->routePrefix, '/');
    parent::init();
}

            
iterateRules() protected method (available since version 2.0.12)

定義於: yii\web\CompositeUrlRule::iterateRules()

迭代指定的規則,並為每個規則呼叫 createUrl()

另請參閱 createUrl()

protected boolean|string iterateRules ( $rules, $manager, $route, $params )
$rules yii\web\UrlRuleInterface[]

要迭代的規則。

$manager yii\web\UrlManager

URL 管理器

$route string

路由。開頭或結尾不應有斜線。

$params array

參數

return boolean|string

建立的 URL,如果沒有指定的規則可以用於建立此 URL,則為 false

                protected function iterateRules($rules, $manager, $route, $params)
{
    /* @var $rule UrlRule */
    foreach ($rules as $rule) {
        $url = $rule->createUrl($manager, $route, $params);
        if ($url !== false) {
            $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
            return $url;
        }
        if (
            $this->createStatus === null
            || !method_exists($rule, 'getCreateUrlStatus')
            || $rule->getCreateUrlStatus() === null
        ) {
            $this->createStatus = null;
        } else {
            $this->createStatus |= $rule->getCreateUrlStatus();
        }
    }
    return false;
}

            
parseRequest() public method

解析給定的請求,並傳回對應的路由和參數。

public array|boolean parseRequest ( $manager, $request )
$manager yii\web\UrlManager

URL 管理器

$request yii\web\Request

請求組件

return array|boolean

解析結果。路由和參數以陣列形式傳回。如果為 false,則表示此規則不能用於解析此路徑資訊。

                public function parseRequest($manager, $request)
{
    $pathInfo = $request->getPathInfo();
    if ($this->prefix === '' || strpos($pathInfo . '/', $this->prefix . '/') === 0) {
        return parent::parseRequest($manager, $request);
    }
    return false;
}