抽象類別 yii\web\CompositeUrlRule
CompositeUrlRule 是由多個簡單規則組成的 URL 規則類別的基底類別。
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$createStatus | integer|null | 上次 createUrl() 呼叫後 URL 建立的狀態。 | yii\web\CompositeUrlRule |
$createUrlStatus | integer|null | 上次 createUrl() 呼叫後 URL 建立的狀態。 | yii\web\CompositeUrlRule |
$rules | yii\web\UrlRuleInterface[] | 複合規則中包含的 URL 規則。 | yii\web\CompositeUrlRule |
公用方法
方法 | 描述 | 定義於 |
---|---|---|
__call() | 呼叫指定的非類別方法。 | yii\base\BaseObject |
__construct() | 建構子。 | yii\base\BaseObject |
__get() | 傳回物件屬性的值。 | yii\base\BaseObject |
__isset() | 檢查屬性是否已設定,亦即已定義且非空值。 | 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\CompositeUrlRule |
getCreateUrlStatus() | 傳回上次 createUrl() 呼叫後 URL 建立的狀態。 | yii\web\CompositeUrlRule |
hasMethod() | 傳回一個值,指示方法是否已定義。 | yii\base\BaseObject |
hasProperty() | 傳回一個值,指示屬性是否已定義。 | yii\base\BaseObject |
init() | 初始化物件。 | yii\web\CompositeUrlRule |
parseRequest() | 解析給定的請求並傳回相應的路由和參數。 | yii\web\CompositeUrlRule |
受保護的方法
方法 | 描述 | 定義於 |
---|---|---|
createRules() | 建立應包含在此複合規則中的 URL 規則。 | yii\web\CompositeUrlRule |
iterateRules() | 迭代指定的規則並為每個規則呼叫 createUrl()。 | yii\web\CompositeUrlRule |
屬性詳細資訊
上次 createUrl() 呼叫後 URL 建立的狀態。
上次 createUrl() 呼叫後 URL 建立的狀態。如果規則未提供建立狀態的資訊,則為 null
。
此複合規則中包含的 URL 規則。此屬性在 init() 中由 createRules() 的傳回值設定。
方法詳細資訊
public mixed __call ( $name, $params ) | ||
$name | 字串 |
方法名稱 |
$params | 陣列 |
方法參數 |
傳回 | 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 | 陣列 |
將用於初始化物件屬性的名稱-值配對 |
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 | 字串 |
屬性名稱 |
傳回 | 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()
檢查屬性是否已設定,亦即已定義且非空值。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($object->property)
時會隱式呼叫。
請注意,如果屬性未定義,將傳回 false。
public boolean __isset ( $name ) | ||
$name | 字串 |
屬性名稱或事件名稱 |
傳回 | 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 | 字串 |
屬性名稱或事件名稱 |
$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 | 字串 |
屬性名稱 |
拋出 | 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 | 字串 |
屬性名稱 |
$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 | 字串 |
屬性名稱 |
$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 ( ) | ||
傳回 | 字串 |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
建立應包含在此複合規則中的 URL 規則。
protected abstract yii\web\UrlRuleInterface[] createRules ( ) | ||
傳回 | yii\web\UrlRuleInterface[] |
URL 規則 |
---|
abstract protected function createRules();
根據給定的路由和參數建立 URL。
public string|boolean createUrl ( $manager, $route, $params ) | ||
$manager | yii\web\UrlManager |
URL 管理器 |
$route | 字串 |
路由。開頭或結尾不應有斜線。 |
$params | 陣列 |
參數 |
傳回 | string|boolean |
建立的 URL,如果此規則無法用於建立此 URL,則為 false。 |
---|
public function createUrl($manager, $route, $params)
{
$this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
$url = $this->iterateRules($this->rules, $manager, $route, $params);
if ($url !== false) {
return $url;
}
if ($this->createStatus === UrlRule::CREATE_STATUS_SUCCESS) {
// create status was not changed - there is no rules configured
$this->createStatus = UrlRule::CREATE_STATUS_PARSING_ONLY;
}
return false;
}
傳回上次 createUrl() 呼叫後 URL 建立的狀態。
對於多個規則,狀態將透過位元 or
運算子組合 (例如 UrlRule::CREATE_STATUS_PARSING_ONLY | UrlRule::CREATE_STATUS_PARAMS_MISMATCH
)。
另請參閱
public integer|null getCreateUrlStatus ( ) | ||
傳回 | integer|null |
上次 createUrl() 呼叫後 URL 建立的狀態。如果規則未提供建立狀態的資訊,則為 |
---|
public function getCreateUrlStatus()
{
return $this->createStatus;
}
定義於: yii\base\BaseObject::hasMethod()
傳回一個值,指示方法是否已定義。
預設實作是對 php 函式 method_exists()
的呼叫。當您實作 php 魔術方法 __call()
時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | 字串 |
方法名稱 |
傳回 | 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 | 字串 |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
傳回 | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
初始化物件。
在物件使用給定的組態初始化後,將在建構子結尾調用此方法。
public void init ( ) |
public function init()
{
parent::init();
$this->rules = $this->createRules();
}
迭代指定的規則並為每個規則呼叫 createUrl()。
另請參閱 createUrl()。
protected boolean|string iterateRules ( $rules, $manager, $route, $params ) | ||
$rules | yii\web\UrlRuleInterface[] |
要迭代的規則。 |
$manager | yii\web\UrlManager |
URL 管理器 |
$route | 字串 |
路由。開頭或結尾不應有斜線。 |
$params | 陣列 |
參數 |
傳回 | boolean|string |
建立的 URL,如果指定的規則都無法用於建立此 URL,則為 |
---|
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;
}
解析給定的請求並傳回相應的路由和參數。
public array|boolean parseRequest ( $manager, $request ) | ||
$manager | yii\web\UrlManager |
URL 管理器 |
$request | yii\web\Request |
請求元件 |
傳回 | array|boolean |
解析結果。路由和參數以陣列形式傳回。如果為 false,則表示此規則無法用於解析此路徑資訊。 |
---|
public function parseRequest($manager, $request)
{
foreach ($this->rules as $rule) {
/* @var $rule UrlRule */
$result = $rule->parseRequest($manager, $request);
if (YII_DEBUG) {
Yii::debug([
'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
'match' => $result !== false,
'parent' => self::className(),
], __METHOD__);
}
if ($result !== false) {
return $result;
}
}
return false;
}
註冊 或 登入 以進行評論。