1 位追蹤者

類別 yii\base\Module

繼承關係yii\base\Module » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject
實作介面yii\base\Configurable
子類別yii\base\Application, yii\console\Application, yii\web\Application
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/base/Module.php

Module 是模組和應用程式類別的基底類別。

一個模組代表一個子應用程式,它本身包含 MVC 元素,例如模型、視圖、控制器等。

一個模組可能由子模組組成。

組件可以向模組註冊,以便它們在模組內全域存取。

有關模組的更多詳細資訊和使用方法,請參閱關於模組的指南文章

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$aliases 陣列 要定義的路徑別名列表。 yii\base\Module
$basePath 字串 模組的根目錄。 yii\base\Module
$behaviors yii\base\Behavior[] 附加到此組件的行為列表。 yii\base\Component
$components 陣列 組件定義或已載入組件實例的列表 (ID => 定義或實例)。 yii\di\ServiceLocator
$controllerMap 陣列 從控制器 ID 到控制器配置的映射。 yii\base\Module
$controllerNamespace 字串|null 控制器類別所在的命名空間。 yii\base\Module
$controllerPath 字串 包含控制器類別的目錄。 yii\base\Module
$defaultRoute 字串 此模組的預設路由。 yii\base\Module
$id 字串 在具有相同父模組的其他模組中,唯一識別此模組的 ID。 yii\base\Module
$layout 字串|布林值|null 應該應用於此模組內視圖的佈局。 yii\base\Module
$layoutPath 字串 佈局檔案的根目錄。 yii\base\Module
$module yii\base\Module|null 此模組的父模組。 yii\base\Module
$modules 陣列 模組 (以其 ID 索引)。 yii\base\Module
$params 陣列 自訂模組參數 (名稱 => 值)。 yii\base\Module
$uniqueId 字串 模組的唯一 ID。 yii\base\Module
$version 字串 此模組的版本。 yii\base\Module
$viewPath 字串 視圖檔案的根目錄。 yii\base\Module

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫指定的非類別方法。 yii\base\Component
__clone() 在通過克隆現有對象創建對象後調用此方法。 yii\base\Component
__construct() 建構子。 yii\base\Module
__get() Getter 魔術方法。 yii\di\ServiceLocator
__isset() 檢查屬性值是否為 null。 yii\di\ServiceLocator
__set() 設定組件屬性的值。 yii\base\Component
__unset() 將組件屬性設定為 null。 yii\base\Component
afterAction() 此方法會在於此模組內執行一個動作之後立即被調用。 yii\base\Module
attachBehavior() 將行為附加到此組件。 yii\base\Component
attachBehaviors() 將行為列表附加到此組件。 yii\base\Component
beforeAction() 此方法會在於此模組內執行一個動作之前立即被調用。 yii\base\Module
behaviors() 返回此組件應表現為的行為列表。 yii\base\Component
canGetProperty() 返回一個值,指示是否可以讀取屬性。 yii\base\Component
canSetProperty() 返回一個值,指示是否可以設定屬性。 yii\base\Component
className() 返回此類別的完整限定名稱。 yii\base\BaseObject
clear() 從定位器中移除組件。 yii\di\ServiceLocator
createController() 根據給定的路由建立控制器實例。 yii\base\Module
createControllerByID() 根據給定的控制器 ID 建立控制器。 yii\base\Module
detachBehavior() 從組件中分離行為。 yii\base\Component
detachBehaviors() 從組件中分離所有行為。 yii\base\Component
ensureBehaviors() 確保在 behaviors() 中宣告的行為已附加到此組件。 yii\base\Component
get() 返回具有指定 ID 的組件實例。 yii\base\Module
getBasePath() 返回模組的根目錄。 yii\base\Module
getBehavior() 返回具名的行為物件。 yii\base\Component
getBehaviors() 返回附加到此組件的所有行為。 yii\base\Component
getComponents() 返回組件定義列表或已載入的組件實例。 yii\di\ServiceLocator
getControllerPath() 根據 $controllerNamespace 返回包含控制器類別的目錄。 yii\base\Module
getInstance() 返回當前請求的此模組類別實例。 yii\base\Module
getLayoutPath() 返回包含此模組版面配置檢視檔案的目錄。 yii\base\Module
getModule() 檢索指定 ID 的子模組。 yii\base\Module
getModules() 返回此模組中的子模組。 yii\base\Module
getUniqueId() 返回一個 ID,在當前應用程式中的所有模組中唯一識別此模組。 yii\base\Module
getVersion() 返回當前模組版本。 yii\base\Module
getViewPath() 返回包含此模組檢視檔案的目錄。 yii\base\Module
has() 返回一個值,指示定位器是否具有指定的組件定義或是否已實例化組件。 yii\base\Module
hasEventHandlers() 返回一個值,指示是否有名稱事件附加任何處理器。 yii\base\Component
hasMethod() 返回一個值,指示是否已定義方法。 yii\base\Component
hasModule() 檢查指定 ID 的子模組是否存在。 yii\base\Module
hasProperty() 返回一個值,指示是否為此組件定義了屬性。 yii\base\Component
init() 初始化模組。 yii\base\Module
off() 從此組件分離現有的事件處理器。 yii\base\Component
on() 將事件處理器附加到事件。 yii\base\Component
runAction() 執行由路由指定的控制器動作。 yii\base\Module
set() 向此定位器註冊組件定義。 yii\di\ServiceLocator
setAliases() 定義路徑別名。 yii\base\Module
setBasePath() 設定模組的根目錄。 yii\base\Module
setComponents() 在此定位器中註冊一組組件定義。 yii\di\ServiceLocator
setControllerPath() 設定包含控制器類別的目錄。 yii\base\Module
setInstance() 設定當前請求的此模組類別實例。 yii\base\Module
setLayoutPath() 設定包含版面配置檔案的目錄。 yii\base\Module
setModule() 向此模組新增子模組。 yii\base\Module
setModules() 在當前模組中註冊子模組。 yii\base\Module
setVersion() 設定當前模組版本。 yii\base\Module
setViewPath() 設定包含檢視檔案的目錄。 yii\base\Module
trigger() 觸發事件。 yii\base\Component

Protected Methods

隱藏繼承的方法

方法 描述 定義於
defaultVersion() 返回預設模組版本。 yii\base\Module

Events

Hide inherited events

Event 類型 描述 定義於
EVENT_AFTER_ACTION yii\base\ActionEvent 在執行控制器動作之後引發的事件。 yii\base\Module
EVENT_BEFORE_ACTION yii\base\ActionEvent 在執行控制器動作之前引發的事件。 yii\base\Module

Property Details

隱藏繼承的屬性

$aliases public property

要定義的路徑別名列表。陣列鍵是別名名稱 (必須以 @ 開頭),而陣列值是相應的路徑或別名。請參閱 setAliases() 以取得範例。

public array $aliases null
$basePath public property

模組的根目錄。

public string $basePath null
$controllerMap public property

從控制器 ID 到控制器配置的映射。每個名稱-值對指定單一控制器的配置。控制器配置可以是字串或陣列。如果是前者,字串應為控制器的完整限定類別名稱。如果是後者,陣列必須包含一個 class 元素,該元素指定控制器的完整限定類別名稱,而陣列中其餘的名稱-值對則用於初始化對應的控制器屬性。例如:

[
  'account' => 'app\controllers\UserController',
  'article' => [
     'class' => 'app\controllers\PostController',
     'pageTitle' => 'something new',
  ],
]
public array $controllerMap = []
$controllerNamespace public property

控制器類別所在的命名空間。此命名空間將用於透過將其前置到控制器類別名稱來載入控制器類別。

如果未設定,它將使用此模組命名空間下的 controllers 子命名空間。例如,如果此模組的命名空間為 foo\bar,則預設控制器命名空間將為 foo\bar\controllers

另請參閱 關於自動載入的指南章節,以了解有關定義命名空間以及類別如何載入的更多資訊。

$controllerPath public property

包含控制器類別的目錄。

public string $controllerPath null
$defaultRoute public property

此模組的預設路由。預設為 default。路由可能包含子模組 ID、控制器 ID 和/或動作 ID。例如:helppost/createadmin/post/create。如果未給定動作 ID,它將採用 yii\base\Controller::$defaultAction 中指定的預設值。

public string $defaultRoute 'default'
$id public property

在具有相同父模組的其他模組中,唯一識別此模組的 ID。

public string $id null
$layout public property

應該應用於此模組內檢視的版面配置。這指的是相對於 $layoutPath 的檢視名稱。如果未設定,則表示將採用 父模組 的版面配置值。如果這是 false,則將在此模組內停用版面配置。

public string|boolean|null $layout null
$layoutPath public property

版面配置檔案的根目錄。預設為 "$viewPath/layouts"。

public string $layoutPath null
$module public property

此模組的父模組。如果此模組沒有父模組,則為 null

$modules public property

模組 (以其 ID 索引)。

public array $modules null
$params public property

自訂模組參數 (名稱 => 值)。

public array $params = []
$uniqueId public property

模組的唯一 ID。

public string $uniqueId null
$version public property

此模組的版本。請注意,此屬性的類型在 getter 和 setter 中有所不同。有關詳細資訊,請參閱 getVersion()setVersion()

public string $version null
$viewPath public property

檢視檔案的根目錄。預設為 "$basePath/views"。

public string $viewPath null

Method Details

隱藏繼承的方法

__call() public method

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

呼叫指定的非類別方法。

此方法將檢查是否有任何附加的行為具有具名方法,並在可用時執行它。

不要直接調用此方法,因為它是 PHP 魔術方法,當調用未知方法時,它將被隱式調用。

public mixed __call ( $name, $params )
$name 字串

方法名稱

$params 陣列

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當調用未知方法時

                public function __call($name, $params)
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $object) {
        if ($object->hasMethod($name)) {
            return call_user_func_array([$object, $name], $params);
        }
    }
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__clone() public method

定義於: yii\base\Component::__clone()

在通過克隆現有對象創建對象後調用此方法。

它會移除所有行為,因為它們附加到舊的物件。

public void __clone ( )

                public function __clone()
{
    $this->_events = [];
    $this->_eventWildcards = [];
    $this->_behaviors = null;
}

            
__construct() public method

建構子。

public void __construct ( $id, $parent null, $config = [] )
$id 字串

此模組的 ID。

$parent yii\base\Module|null

父模組 (如果有的話)。

$config 陣列

將用於初始化物件屬性的名稱-值對。

                public function __construct($id, $parent = null, $config = [])
{
    $this->id = $id;
    $this->module = $parent;
    parent::__construct($config);
}

            
__get() public method

定義於: yii\di\ServiceLocator::__get()

Getter 魔術方法。

覆寫此方法以支援像讀取屬性一樣存取組件。

public mixed __get ( $name )
$name 字串

組件或屬性名稱

return mixed

具名屬性值

                public function __get($name)
{
    if ($this->has($name)) {
        return $this->get($name);
    }
    return parent::__get($name);
}

            
__isset() public method

定義於: yii\di\ServiceLocator::__isset()

檢查屬性值是否為 null。

此方法透過檢查具名組件是否已載入來覆寫父實作。

public boolean __isset ( $name )
$name 字串

屬性名稱或事件名稱

return boolean

屬性值是否為 null

                public function __isset($name)
{
    if ($this->has($name)) {
        return true;
    }
    return parent::__isset($name);
}

            
__set() public method

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

設定組件屬性的值。

此方法將依以下順序檢查並採取相應措施

  • 由 setter 定義的屬性:設定屬性值
  • 格式為 "on xyz" 的事件:將處理器附加到事件 "xyz"
  • 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
  • 行為的屬性:設定行為屬性值

不要直接調用此方法,因為它是 PHP 魔術方法,當執行 $component->property = $value; 時,它將被隱式調用。

另請參閱 __get()

public void __set ( $name, $value )
$name 字串

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性為唯讀。

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // set property
        $this->$setter($value);
        return;
    } elseif (strncmp($name, 'on ', 3) === 0) {
        // on event: attach event handler
        $this->on(trim(substr($name, 3)), $value);
        return;
    } elseif (strncmp($name, 'as ', 3) === 0) {
        // as behavior: attach behavior
        $name = trim(substr($name, 3));
        $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = $value;
            return;
        }
    }
    if (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}

            
__unset() public method

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

將組件屬性設定為 null。

此方法將依以下順序檢查並採取相應措施

  • 由 setter 定義的屬性:將屬性值設定為 null
  • 行為的屬性:將屬性值設定為 null

不要直接調用此方法,因為它是 PHP 魔術方法,當執行 unset($component->property) 時,它將被隱式調用。

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

public void __unset ( $name )
$name 字串

屬性名稱

throws yii\base\InvalidCallException

如果屬性為唯讀。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = null;
            return;
        }
    }
    throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}

            
afterAction() public method

此方法會在於此模組內執行一個動作之後立即被調用。

此方法將觸發 EVENT_AFTER_ACTION 事件。此方法的傳回值將用作動作傳回值。

如果您覆寫此方法,您的程式碼應如下所示

public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);
    // your custom code here
    return $result;
}
public mixed afterAction ( $action, $result )
$action yii\base\Action

剛執行的動作。

$result mixed

動作傳回結果。

return mixed

已處理的動作結果。

                public function afterAction($action, $result)
{
    $event = new ActionEvent($action);
    $event->result = $result;
    $this->trigger(self::EVENT_AFTER_ACTION, $event);
    return $event->result;
}

            
attachBehavior() public method

定義於: yii\base\Component::attachBehavior()

將行為附加到此組件。

此方法將根據給定的配置建立行為物件。之後,將透過調用 yii\base\Behavior::attach() 方法將行為物件附加到此組件。

另請參閱 detachBehavior()

public yii\base\Behavior attachBehavior ( $name, $behavior )
$name 字串

行為的名稱。

$behavior string|array|yii\base\Behavior

行為設定。這可以是下列其中一種

return yii\base\Behavior

行為物件

                public function attachBehavior($name, $behavior)
{
    $this->ensureBehaviors();
    return $this->attachBehaviorInternal($name, $behavior);
}

            
attachBehaviors() public method

定義於: yii\base\Component::attachBehaviors()

將行為列表附加到此組件。

每個行為都以其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或是用於建立行為的設定陣列。

另請參閱 attachBehavior()

public void attachBehaviors ( $behaviors )
$behaviors 陣列

要附加到元件的行為列表

                public function attachBehaviors($behaviors)
{
    $this->ensureBehaviors();
    foreach ($behaviors as $name => $behavior) {
        $this->attachBehaviorInternal($name, $behavior);
    }
}

            
beforeAction() public method

此方法會在於此模組內執行一個動作之前立即被調用。

此方法將觸發 EVENT_BEFORE_ACTION 事件。此方法的傳回值將決定是否應繼續執行動作。

如果動作不應執行,則應在 beforeAction 程式碼內處理請求,方法是提供必要的輸出或重新導向請求。否則,回應將為空。

如果您覆寫此方法,您的程式碼應如下所示

public function beforeAction($action)
{
    if (!parent::beforeAction($action)) {
        return false;
    }

    // your custom code here

    return true; // or false to not run the action
}
public boolean beforeAction ( $action )
$action yii\base\Action

要執行的動作。

return boolean

是否應繼續執行動作。

                public function beforeAction($action)
{
    $event = new ActionEvent($action);
    $this->trigger(self::EVENT_BEFORE_ACTION, $event);
    return $event->isValid;
}

            
behaviors() public method

定義於: yii\base\Component::behaviors()

返回此組件應表現為的行為列表。

子類別可以覆寫此方法,以指定它們想要表現為的行為。

此方法的傳回值應為行為物件或設定的陣列,並以行為名稱索引。行為設定可以是指定行為類別的字串,或是具有以下結構的陣列

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

請注意,行為類別必須從 yii\base\Behavior 擴展而來。行為可以使用名稱或匿名方式附加。當名稱用作陣列索引鍵時,可以使用此名稱稍後使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。匿名行為無法檢索或分離。

在此方法中宣告的行為將自動附加到元件(依需求)。

public array behaviors ( )
return 陣列

行為設定。

                public function behaviors()
{
    return [];
}

            
canGetProperty() public method

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

返回一個值,指示是否可以讀取屬性。

如果符合以下條件,則可以讀取屬性

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

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此元件的屬性

return boolean

屬性是否可以讀取

                public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canGetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
canSetProperty() public method

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

返回一個值,指示是否可以設定屬性。

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

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

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此元件的屬性

return boolean

屬性是否可以寫入

                public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canSetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

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

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

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

public static string className ( )
return 字串

此類別的完整限定名稱。

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

            
clear() public method

定義於: yii\di\ServiceLocator::clear()

從定位器中移除組件。

public void clear ( $id )
$id 字串

元件 ID

                public function clear($id)
{
    unset($this->_definitions[$id], $this->_components[$id]);
}

            
createController() public method

根據給定的路由建立控制器實例。

路由應相對於此模組。此方法實作以下演算法來解析給定的路由

  1. 如果路由為空,則使用 $defaultRoute
  2. 如果在 $controllerMap 中找到路由的第一個區段,則根據 $controllerMap 中找到的相應設定建立控制器;
  3. 如果路由的第一個區段是在 $modules 中宣告的有效模組 ID,則使用路由的其餘部分呼叫模組的 createController()
  4. 給定的路由格式為 abc/def/xyz。嘗試在 控制器命名空間 內使用 abc\DefControllerabc\def\XyzController 類別。

如果以上任何步驟解析為控制器,則會與路由的其餘部分一起傳回,路由的其餘部分將被視為動作 ID。否則,將傳回 false

public array|boolean createController ( $route )
$route 字串

由模組、控制器和動作 ID 組成的路由。

return array|boolean

如果控制器建立成功,則會與請求的動作 ID 一起傳回。否則將傳回 false

throws yii\base\InvalidConfigException

如果控制器類別及其檔案不符。

                public function createController($route)
{
    if ($route === '') {
        $route = $this->defaultRoute;
    }
    // double slashes or leading/ending slashes may cause substr problem
    $route = trim($route, '/');
    if (strpos($route, '//') !== false) {
        return false;
    }
    if (strpos($route, '/') !== false) {
        list($id, $route) = explode('/', $route, 2);
    } else {
        $id = $route;
        $route = '';
    }
    // module and controller map take precedence
    if (isset($this->controllerMap[$id])) {
        $controller = Yii::createObject($this->controllerMap[$id], [$id, $this]);
        return [$controller, $route];
    }
    $module = $this->getModule($id);
    if ($module !== null) {
        return $module->createController($route);
    }
    if (($pos = strrpos($route, '/')) !== false) {
        $id .= '/' . substr($route, 0, $pos);
        $route = substr($route, $pos + 1);
    }
    $controller = $this->createControllerByID($id);
    if ($controller === null && $route !== '') {
        $controller = $this->createControllerByID($id . '/' . $route);
        $route = '';
    }
    return $controller === null ? false : [$controller, $route];
}

            
createControllerByID() public method

根據給定的控制器 ID 建立控制器。

控制器 ID 相對於此模組。控制器類別應命名空間在 $controllerNamespace 下。

請注意,此方法不檢查 $modules$controllerMap

public yii\base\Controller|null createControllerByID ( $id )
$id 字串

控制器 ID。

return yii\base\Controller|null

新建立的控制器實例,如果控制器 ID 無效,則為 null

throws yii\base\InvalidConfigException

如果控制器類別及其檔案名稱不符。此例外僅在偵錯模式下拋出。

                public function createControllerByID($id)
{
    $pos = strrpos($id, '/');
    if ($pos === false) {
        $prefix = '';
        $className = $id;
    } else {
        $prefix = substr($id, 0, $pos + 1);
        $className = substr($id, $pos + 1);
    }
    if ($this->isIncorrectClassNameOrPrefix($className, $prefix)) {
        return null;
    }
    $className = preg_replace_callback('%-([a-z0-9_])%i', function ($matches) {
            return ucfirst($matches[1]);
    }, ucfirst($className)) . 'Controller';
    $className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
    if (strpos($className, '-') !== false || !class_exists($className)) {
        return null;
    }
    if (is_subclass_of($className, 'yii\base\Controller')) {
        $controller = Yii::createObject($className, [$id, $this]);
        return get_class($controller) === $className ? $controller : null;
    } elseif (YII_DEBUG) {
        throw new InvalidConfigException('Controller class must extend from \\yii\\base\\Controller.');
    }
    return null;
}

            
defaultVersion() protected method (available since version 2.0.11)

返回預設模組版本。

子類別可以覆寫此方法以提供更具體的版本偵測。

protected string defaultVersion ( )
return 字串

此模組的版本。

                protected function defaultVersion()
{
    if ($this->module === null) {
        return '1.0';
    }
    return $this->module->getVersion();
}

            
detachBehavior() public method

定義於: yii\base\Component::detachBehavior()

從組件中分離行為。

將調用行為的 yii\base\Behavior::detach() 方法。

public yii\base\Behavior|null detachBehavior ( $name )
$name 字串

行為的名稱。

return yii\base\Behavior|null

已分離的行為。如果行為不存在,則為 Null。

                public function detachBehavior($name)
{
    $this->ensureBehaviors();
    if (isset($this->_behaviors[$name])) {
        $behavior = $this->_behaviors[$name];
        unset($this->_behaviors[$name]);
        $behavior->detach();
        return $behavior;
    }
    return null;
}

            
detachBehaviors() public method

定義於: yii\base\Component::detachBehaviors()

從組件中分離所有行為。

public void detachBehaviors ( )

                public function detachBehaviors()
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $name => $behavior) {
        $this->detachBehavior($name);
    }
}

            
ensureBehaviors() public method

定義於: yii\base\Component::ensureBehaviors()

確保在 behaviors() 中宣告的行為已附加到此組件。

public void ensureBehaviors ( )

                public function ensureBehaviors()
{
    if ($this->_behaviors === null) {
        $this->_behaviors = [];
        foreach ($this->behaviors() as $name => $behavior) {
            $this->attachBehaviorInternal($name, $behavior);
        }
    }
}

            
get() public method

返回具有指定 ID 的組件實例。

自 2.0.13 版本起,如果模組中未定義元件,則會在父模組中查找。父模組可能是應用程式。

public object|null get ( $id, $throwException true )
$id 字串

元件 ID(例如 db)。

$throwException boolean

如果 $id 未事先向定位器註冊,是否拋出例外。

return object|null

指定 ID 的元件。如果 $throwException 為 false 且 $id 事先未註冊,則將傳回 null。

throws yii\base\InvalidConfigException

如果 $id 參照不存在的元件 ID

                public function get($id, $throwException = true)
{
    if (!isset($this->module)) {
        return parent::get($id, $throwException);
    }
    $component = parent::get($id, false);
    if ($component === null) {
        $component = $this->module->get($id, $throwException);
    }
    return $component;
}

            
getBasePath() public method

返回模組的根目錄。

預設為包含模組類別檔案的目錄。

public string getBasePath ( )
return 字串

模組的根目錄。

                public function getBasePath()
{
    if ($this->_basePath === null) {
        $class = new \ReflectionClass($this);
        $this->_basePath = dirname($class->getFileName());
    }
    return $this->_basePath;
}

            
getBehavior() public method

定義於: yii\base\Component::getBehavior()

返回具名的行為物件。

public yii\base\Behavior|null getBehavior ( $name )
$name 字串

行為名稱

return yii\base\Behavior|null

行為物件,如果行為不存在,則為 null

                public function getBehavior($name)
{
    $this->ensureBehaviors();
    return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}

            
getBehaviors() public method

定義於: yii\base\Component::getBehaviors()

返回附加到此組件的所有行為。

public yii\base\Behavior[] getBehaviors ( )
return yii\base\Behavior[]

附加到此元件的行為列表

                public function getBehaviors()
{
    $this->ensureBehaviors();
    return $this->_behaviors;
}

            
getComponents() public method

定義於: yii\di\ServiceLocator::getComponents()

返回組件定義列表或已載入的組件實例。

public array getComponents ( $returnDefinitions true )
$returnDefinitions boolean

是否傳回元件定義而不是已載入的元件實例。

return 陣列

組件定義或已載入組件實例的列表 (ID => 定義或實例)。

                public function getComponents($returnDefinitions = true)
{
    return $returnDefinitions ? $this->_definitions : $this->_components;
}

            
getControllerPath() public method

根據 $controllerNamespace 返回包含控制器類別的目錄。

請注意,為了使此方法傳回值,您必須為 $controllerNamespace 的根命名空間定義別名。

public string getControllerPath ( )
return 字串

包含控制器類別的目錄。

throws yii\base\InvalidArgumentException

如果未為 $controllerNamespace 的根命名空間定義別名。

                public function getControllerPath()
{
    if ($this->_controllerPath === null) {
        $this->_controllerPath = Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
    }
    return $this->_controllerPath;
}

            
getInstance() public static method

返回當前請求的此模組類別實例。

如果目前未請求模組類別,則將傳回 null。提供此方法是為了讓您可以從模組內的任何位置存取模組實例。

public static static|null getInstance ( )
return yii\base\Module|null

此模組類別目前請求的實例,如果未請求模組類別,則為 null

                public static function getInstance()
{
    $class = get_called_class();
    return isset(Yii::$app->loadedModules[$class]) ? Yii::$app->loadedModules[$class] : null;
}

            
getLayoutPath() public method

返回包含此模組版面配置檢視檔案的目錄。

public string getLayoutPath ( )
return 字串

版面配置檔案的根目錄。預設為 "$viewPath/layouts"。

                public function getLayoutPath()
{
    if ($this->_layoutPath === null) {
        $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';
    }
    return $this->_layoutPath;
}

            
getModule() public method

檢索指定 ID 的子模組。

此方法支援檢索子模組和孫子模組。

另請參閱 hasModule()

public yii\base\Module|null getModule ( $id, $load true )
$id 字串

模組 ID(區分大小寫)。若要檢索孫子模組,請使用相對於此模組的 ID 路徑(例如 admin/content)。

$load boolean

是否在模組尚未載入時載入模組。

return yii\base\Module|null

模組實例,如果模組不存在,則為 null

                public function getModule($id, $load = true)
{
    if (($pos = strpos($id, '/')) !== false) {
        // sub-module
        $module = $this->getModule(substr($id, 0, $pos));
        return $module === null ? null : $module->getModule(substr($id, $pos + 1), $load);
    }
    if (isset($this->_modules[$id])) {
        if ($this->_modules[$id] instanceof self) {
            return $this->_modules[$id];
        } elseif ($load) {
            Yii::debug("Loading module: $id", __METHOD__);
            /* @var $module Module */
            $module = Yii::createObject($this->_modules[$id], [$id, $this]);
            $module::setInstance($module);
            return $this->_modules[$id] = $module;
        }
    }
    return null;
}

            
getModules() public method

返回此模組中的子模組。

public array getModules ( $loadedOnly false )
$loadedOnly boolean

是否僅傳回已載入的子模組。如果設定為 false,則會傳回在此模組中註冊的所有子模組,無論它們是否已載入。已載入的模組將作為物件傳回,而未載入的模組將作為設定陣列傳回。

return 陣列

模組 (以其 ID 索引)。

                public function getModules($loadedOnly = false)
{
    if ($loadedOnly) {
        $modules = [];
        foreach ($this->_modules as $module) {
            if ($module instanceof self) {
                $modules[] = $module;
            }
        }
        return $modules;
    }
    return $this->_modules;
}

            
getUniqueId() public method

返回一個 ID,在當前應用程式中的所有模組中唯一識別此模組。

請注意,如果模組是應用程式,則會傳回空字串。

public string getUniqueId ( )
return 字串

模組的唯一 ID。

                public function getUniqueId()
{
    return $this->module ? ltrim($this->module->getUniqueId() . '/' . $this->id, '/') : $this->id;
}

            
getVersion() public method (available since version 2.0.11)

返回當前模組版本。

如果未明確設定版本,則將使用 defaultVersion() 方法來判斷其值。

public string getVersion ( )
return 字串

此模組的版本。

                public function getVersion()
{
    if ($this->_version === null) {
        $this->_version = $this->defaultVersion();
    } else {
        if (!is_scalar($this->_version)) {
            $this->_version = call_user_func($this->_version, $this);
        }
    }
    return $this->_version;
}

            
getViewPath() public method

返回包含此模組檢視檔案的目錄。

public string getViewPath ( )
return 字串

檢視檔案的根目錄。預設為 "$basePath/views"。

                public function getViewPath()
{
    if ($this->_viewPath === null) {
        $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';
    }
    return $this->_viewPath;
}

            
has() public method

返回一個值,指示定位器是否具有指定的組件定義或是否已實例化組件。

自 2.0.13 版本起,如果模組中未定義元件,則會在父模組中查找。父模組可能是應用程式。

此方法可能會根據 $checkInstance 的值返回不同的結果。

  • 如果 $checkInstance 為 false (預設值),此方法將返回一個值,指示定位器是否具有指定的元件定義。
  • 如果 $checkInstance 為 true,此方法將返回一個值,指示定位器是否已實例化指定的元件。
public boolean has ( $id, $checkInstance false )
$id 字串

元件 ID(例如 db)。

$checkInstance boolean

此方法是否應檢查元件是否為共享且已實例化。

return boolean

定位器是否具有指定的元件定義,或是否已實例化該元件。

                public function has($id, $checkInstance = false)
{
    return parent::has($id, $checkInstance) || (isset($this->module) && $this->module->has($id, $checkInstance));
}

            
hasEventHandlers() public method

定義於: yii\base\Component::hasEventHandlers()

返回一個值,指示是否有名稱事件附加任何處理器。

public boolean hasEventHandlers ( $name )
$name 字串

事件名稱

return boolean

是否有任何處理器附加到事件。

                public function hasEventHandlers($name)
{
    $this->ensureBehaviors();
    if (!empty($this->_events[$name])) {
        return true;
    }
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
            return true;
        }
    }
    return Event::hasHandlers($this, $name);
}

            
hasMethod() public method

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

返回一個值,指示是否已定義方法。

如果符合以下條件,則定義方法:

  • 類別具有指定名稱的方法
  • 附加的行為具有給定名稱的方法 (當 $checkBehaviors 為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors true )
$name 字串

屬性名稱

$checkBehaviors boolean

是否將行為的方法視為此元件的方法

return boolean

方法是否已定義

                public function hasMethod($name, $checkBehaviors = true)
{
    if (method_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->hasMethod($name)) {
                return true;
            }
        }
    }
    return false;
}

            
hasModule() public method

檢查指定 ID 的子模組是否存在。

此方法支援檢查子模組和孫子模組是否存在。

public boolean hasModule ( $id )
$id 字串

模組 ID。對於孫子模組,請使用相對於此模組的 ID 路徑 (例如 admin/content)。

return boolean

具名模組是否存在。已載入和未載入的模組都會被考慮在內。

                public function hasModule($id)
{
    if (($pos = strpos($id, '/')) !== false) {
        // sub-module
        $module = $this->getModule(substr($id, 0, $pos));
        return $module === null ? false : $module->hasModule(substr($id, $pos + 1));
    }
    return isset($this->_modules[$id]);
}

            
hasProperty() public method

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

返回一個值,指示是否為此組件定義了屬性。

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

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

參見

public boolean hasProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此元件的屬性

return boolean

屬性是否已定義

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

            
init() public method

初始化模組。

此方法在模組建立並使用配置中給定的屬性值初始化後呼叫。如果未設定,預設實作將初始化 $controllerNamespace

如果您覆寫此方法,請確保您呼叫父實作。

public void init ( )

                public function init()
{
    if ($this->controllerNamespace === null) {
        $class = get_class($this);
        if (($pos = strrpos($class, '\\')) !== false) {
            $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers';
        }
    }
}

            
off() public method

定義於: yii\base\Component::off()

從此組件分離現有的事件處理器。

此方法與 on() 相反。

注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理器,而使用與此萬用字元匹配的純名稱註冊的處理器將保持不變。

參見 on()

public boolean off ( $name, $handler null )
$name 字串

事件名稱

$handler callable|null

要移除的事件處理器。如果為 null,則會移除附加到具名事件的所有處理器。

return boolean

如果找到並分離處理器

                public function off($name, $handler = null)
{
    $this->ensureBehaviors();
    if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
        return false;
    }
    if ($handler === null) {
        unset($this->_events[$name], $this->_eventWildcards[$name]);
        return true;
    }
    $removed = false;
    // plain event names
    if (isset($this->_events[$name])) {
        foreach ($this->_events[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_events[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_events[$name] = array_values($this->_events[$name]);
            return true;
        }
    }
    // wildcard event names
    if (isset($this->_eventWildcards[$name])) {
        foreach ($this->_eventWildcards[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_eventWildcards[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
            // remove empty wildcards to save future redundant regex checks:
            if (empty($this->_eventWildcards[$name])) {
                unset($this->_eventWildcards[$name]);
            }
        }
    }
    return $removed;
}

            
on() public method

定義於: yii\base\Component::on()

將事件處理器附加到事件。

事件處理器必須是有效的 PHP 回呼。以下是一些範例

function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()

事件處理器必須使用以下簽名定義:

function ($event)

其中 $event 是一個 yii\base\Event 物件,其中包含與事件相關聯的參數。

自 2.0.14 版起,您可以將事件名稱指定為萬用字元模式

$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});

參見 off()

public void on ( $name, $handler, $data null, $append true )
$name 字串

事件名稱

$handler callable

事件處理器

$data mixed

事件觸發時要傳遞給事件處理器的資料。當調用事件處理器時,可以透過 yii\base\Event::$data 存取此資料。

$append boolean

是否將新的事件處理器附加到現有處理器列表的末尾。如果為 false,則新的處理器將插入到現有處理器列表的開頭。

                public function on($name, $handler, $data = null, $append = true)
{
    $this->ensureBehaviors();
    if (strpos($name, '*') !== false) {
        if ($append || empty($this->_eventWildcards[$name])) {
            $this->_eventWildcards[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_eventWildcards[$name], [$handler, $data]);
        }
        return;
    }
    if ($append || empty($this->_events[$name])) {
        $this->_events[$name][] = [$handler, $data];
    } else {
        array_unshift($this->_events[$name], [$handler, $data]);
    }
}

            
runAction() public method

執行由路由指定的控制器動作。

此方法剖析指定的路由,並建立對應的子模組、控制器和動作實例。然後,它會呼叫 yii\base\Controller::runAction(),以使用給定的參數執行動作。如果路由為空,則此方法將使用 $defaultRoute

public mixed runAction ( $route, $params = [] )
$route 字串

指定動作的路由。

$params 陣列

要傳遞給動作的參數

return mixed

動作的結果。

throws yii\base\InvalidRouteException

如果請求的路由無法成功解析為動作。

                public function runAction($route, $params = [])
{
    $parts = $this->createController($route);
    if (is_array($parts)) {
        /* @var $controller Controller */
        list($controller, $actionID) = $parts;
        $oldController = Yii::$app->controller;
        Yii::$app->controller = $controller;
        $result = $controller->runAction($actionID, $params);
        if ($oldController !== null) {
            Yii::$app->controller = $oldController;
        }
        return $result;
    }
    $id = $this->getUniqueId();
    throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');
}

            
set() public method

定義於: yii\di\ServiceLocator::set()

向此定位器註冊組件定義。

例如:

// a class name
$locator->set('cache', 'yii\caching\FileCache');

// a configuration array
$locator->set('db', [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
]);

// an anonymous function
$locator->set('cache', function ($params) {
    return new \yii\caching\FileCache;
});

// an instance
$locator->set('cache', new \yii\caching\FileCache);

如果具有相同 ID 的元件定義已存在,則會覆寫它。

public void set ( $id, $definition )
$id 字串

元件 ID(例如 db)。

$definition mixed

要向此定位器註冊的元件定義。它可以是以下其中之一

  • 類別名稱
  • 組態陣列:陣列包含名稱-值對,當呼叫 get() 時,將使用這些名稱-值對初始化新建立物件的屬性值。class 元素是必要的,代表要建立的物件的類別。
  • PHP 可呼叫物件:匿名函數或表示類別方法的陣列 (例如 ['Foo', 'bar'])。可呼叫物件將由 get() 呼叫,以傳回與指定元件 ID 相關聯的物件。
  • 物件:當呼叫 get() 時,將傳回此物件。
throws yii\base\InvalidConfigException

如果定義是無效的組態陣列

                public function set($id, $definition)
{
    unset($this->_components[$id]);
    if ($definition === null) {
        unset($this->_definitions[$id]);
        return;
    }
    if (is_object($definition) || is_callable($definition, true)) {
        // an object, a class name, or a PHP callable
        $this->_definitions[$id] = $definition;
    } elseif (is_array($definition)) {
        // a configuration array
        if (isset($definition['__class'])) {
            $this->_definitions[$id] = $definition;
            $this->_definitions[$id]['class'] = $definition['__class'];
            unset($this->_definitions[$id]['__class']);
        } elseif (isset($definition['class'])) {
            $this->_definitions[$id] = $definition;
        } else {
            throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element.");
        }
    } else {
        throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
    }
}

            
setAliases() public method

定義路徑別名。

此方法呼叫 Yii::setAlias() 以註冊路徑別名。提供此方法是為了讓您可以在配置模組時定義路徑別名。

public void setAliases ( $aliases )
$aliases 陣列

要定義的路徑別名列表。陣列鍵是別名名稱 (必須以 @ 開頭),陣列值是相應的路徑或別名。例如:

[
    '@models' => '@app/models', // an existing alias
    '@backend' => __DIR__ . '/../backend',  // a directory
]

                public function setAliases($aliases)
{
    foreach ($aliases as $name => $alias) {
        Yii::setAlias($name, $alias);
    }
}

            
setBasePath() public method

設定模組的根目錄。

此方法只能在建構函式的開頭調用。

public void setBasePath ( $path )
$path 字串

模組的根目錄。它可以是目錄名稱或 路徑別名

throws yii\base\InvalidArgumentException

如果目錄不存在。

                public function setBasePath($path)
{
    $path = Yii::getAlias($path);
    $p = strncmp($path, 'phar://', 7) === 0 ? $path : realpath($path);
    if (is_string($p) && is_dir($p)) {
        $this->_basePath = $p;
    } else {
        throw new InvalidArgumentException("The directory does not exist: $path");
    }
}

            
setComponents() public method

定義於: yii\di\ServiceLocator::setComponents()

在此定位器中註冊一組組件定義。

這是 set() 的批量版本。參數應為陣列,其鍵為元件 ID,值為對應的元件定義。

有關如何指定元件 ID 和定義的更多詳細資訊,請參閱 set()

如果具有相同 ID 的元件定義已存在,則會覆寫它。

以下是註冊兩個元件定義的範例

[
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'sqlite:path/to/file.db',
    ],
    'cache' => [
        'class' => 'yii\caching\DbCache',
        'db' => 'db',
    ],
]
public void setComponents ( $components )
$components 陣列

元件定義或實例

                public function setComponents($components)
{
    foreach ($components as $id => $component) {
        $this->set($id, $component);
    }
}

            
setControllerPath() public method (available since version 2.0.44)

設定包含控制器類別的目錄。

public void setControllerPath ( $path )
$path 字串

包含控制器類別的根目錄。

throws yii\base\InvalidArgumentException

如果目錄無效。

                public function setControllerPath($path)
{
    $this->_controllerPath = Yii::getAlias($path);
}

            
setInstance() public static method

設定當前請求的此模組類別實例。

public static void setInstance ( $instance )
$instance yii\base\Module|null

此模組類別目前請求的實例。如果為 null,則將移除呼叫類別的實例 (如果有的話)。

                public static function setInstance($instance)
{
    if ($instance === null) {
        unset(Yii::$app->loadedModules[get_called_class()]);
    } else {
        Yii::$app->loadedModules[get_class($instance)] = $instance;
    }
}

            
setLayoutPath() public method

設定包含版面配置檔案的目錄。

public void setLayoutPath ( $path )
$path 字串

版面配置檔案的根目錄或 路徑別名

throws yii\base\InvalidArgumentException

如果目錄無效

                public function setLayoutPath($path)
{
    $this->_layoutPath = Yii::getAlias($path);
}

            
setModule() public method

向此模組新增子模組。

public void setModule ( $id, $module )
$id 字串

模組 ID。

$module yii\base\Module|array|null

要新增到此模組的子模組。它可以是以下其中之一

  • yii\base\Module 物件
  • 組態陣列:當最初呼叫 getModule() 時,將使用此陣列來實例化子模組
  • null:具名的子模組將從此模組中移除

                public function setModule($id, $module)
{
    if ($module === null) {
        unset($this->_modules[$id]);
    } else {
        $this->_modules[$id] = $module;
        if ($module instanceof self) {
            $module->module = $this;
        }
    }
}

            
setModules() public method

在當前模組中註冊子模組。

每個子模組都應指定為名稱-值對,其中名稱指的是模組的 ID,值指的是模組或可用於建立模組的組態陣列。在後一種情況下,將使用 Yii::createObject() 來建立模組。

如果新的子模組與現有子模組具有相同的 ID,則現有子模組將被靜默覆寫。

以下是註冊兩個子模組的範例

[
    'comment' => [
        'class' => 'app\modules\comment\CommentModule',
        'db' => 'db',
    ],
    'booking' => ['class' => 'app\modules\booking\BookingModule'],
]
public void setModules ( $modules )
$modules 陣列

模組 (id => 模組組態或實例)。

                public function setModules($modules)
{
    foreach ($modules as $id => $module) {
        $this->_modules[$id] = $module;
        if ($module instanceof self) {
            $module->module = $this;
        }
    }
}

            
setVersion() public method (available since version 2.0.11)

設定當前模組版本。

public void setVersion ( $version )
$version string|callable|null

此模組的版本。版本可以指定為 PHP 回呼,它可以接受模組實例作為參數,並且應傳回實際版本。例如:

function (Module $module) {
    //return string
}

                public function setVersion($version)
{
    $this->_version = $version;
}

            
setViewPath() public method

設定包含檢視檔案的目錄。

public void setViewPath ( $path )
$path 字串

視圖檔案的根目錄。

throws yii\base\InvalidArgumentException

如果目錄無效。

                public function setViewPath($path)
{
    $this->_viewPath = Yii::getAlias($path);
}

            
trigger() public method

定義於: yii\base\Component::trigger()

觸發事件。

此方法表示事件的發生。它會調用事件的所有附加處理器,包括類別層級的處理器。

public void trigger ( $name, yii\base\Event $event null )
$name 字串

事件名稱

$event yii\base\Event|null

事件實例。如果未設定,將建立預設的 yii\base\Event 物件。

                public function trigger($name, Event $event = null)
{
    $this->ensureBehaviors();
    $eventHandlers = [];
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (StringHelper::matchWildcard($wildcard, $name)) {
            $eventHandlers[] = $handlers;
        }
    }
    if (!empty($this->_events[$name])) {
        $eventHandlers[] = $this->_events[$name];
    }
    if (!empty($eventHandlers)) {
        $eventHandlers = call_user_func_array('array_merge', $eventHandlers);
        if ($event === null) {
            $event = new Event();
        }
        if ($event->sender === null) {
            $event->sender = $this;
        }
        $event->handled = false;
        $event->name = $name;
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            // stop further handling if the event is handled
            if ($event->handled) {
                return;
            }
        }
    }
    // invoke class-level attached handlers
    Event::trigger($this, $name, $event);
}

            

事件詳細資訊

隱藏繼承的屬性

EVENT_AFTER_ACTION event of type yii\base\ActionEvent

在執行控制器動作之後引發的事件。

EVENT_BEFORE_ACTION event of type yii\base\ActionEvent

在執行控制器動作之前引發的事件。您可以設定 yii\base\ActionEvent::$isValidfalse 以取消動作執行。