0 位追蹤者

抽象類別 yii\base\Application

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

Application 是所有應用程式類別的基底類別。

關於 Application 的更多細節與使用資訊,請參閱 應用程式指南文章

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$aliases array 要定義的路徑別名列表。 yii\base\Module
$assetManager yii\web\AssetManager 資源管理器應用程式組件。 yii\base\Application
$authManager yii\rbac\ManagerInterface|null 權限管理器應用程式組件,如果未配置則為 null。 yii\base\Application
$basePath string 應用程式的根目錄。 yii\base\Application
$behaviors yii\base\Behavior[] 附加到此組件的行為列表。 yii\base\Component
$bootstrap array 在應用程式 啟動過程 中應執行的組件列表。 yii\base\Application
$cache yii\caching\CacheInterface|null 快取應用程式組件。 yii\base\Application
$charset string 應用程式目前使用的字元集。 yii\base\Application
$components array 組件定義或已載入的組件實例列表 (ID => 定義或實例)。 yii\di\ServiceLocator
$container array 以名稱-值對形式給定的值。 yii\base\Application
$controller yii\base\Controller 目前活動的控制器實例 yii\base\Application
$controllerMap array 從控制器 ID 到控制器配置的映射。 yii\base\Module
$controllerNamespace string 控制器類別所在的命名空間。 yii\base\Application
$controllerPath string 包含控制器類別的目錄。 yii\base\Module
$db yii\db\Connection 資料庫連線。 yii\base\Application
$defaultRoute string 此模組的預設路由。 yii\base\Module
$errorHandler yii\web\ErrorHandler|yii\console\ErrorHandler 錯誤處理器應用程式組件。 yii\base\Application
$extensions array|null 已安裝的 Yii 擴展列表。 yii\base\Application
$formatter yii\i18n\Formatter 格式化器應用程式組件。 yii\base\Application
$i18n yii\i18n\I18N 國際化應用程式組件。 yii\base\Application
$id string 在具有相同 父模組 的其他模組中,唯一識別此模組的 ID。 yii\base\Module
$language string 預期用於終端使用者的語言。 yii\base\Application
$layout string|boolean 應套用於此應用程式中視圖的佈局。 yii\base\Application
$layoutPath string 佈局檔案的根目錄。 yii\base\Module
$loadedModules array 依其類別名稱索引的已載入模組列表。 yii\base\Application
$log yii\log\Dispatcher 日誌分發器應用程式組件。 yii\base\Application
$mailer yii\mail\MailerInterface 郵件器應用程式組件。 yii\base\Application
$module yii\base\Module|null 此模組的父模組。 yii\base\Module
$modules array 模組(以它們的 ID 索引)。 yii\base\Module
$name string 應用程式名稱。 yii\base\Application
$params array 自訂模組參數 (名稱 => 值)。 yii\base\Module
$request yii\web\Request|yii\console\Request 請求元件。 yii\base\Application
$requestedAction yii\base\Action|null 請求的 Action。 yii\base\Application
$requestedParams array 提供給請求的 action 的參數。 yii\base\Application
$requestedRoute string 請求的路徑 yii\base\Application
$response yii\web\Response|yii\console\Response 回應元件。 yii\base\Application
$runtimePath string 儲存執行階段檔案的目錄。 yii\base\Application
$security yii\base\Security 安全性應用程式元件。 yii\base\Application
$sourceLanguage string 應用程式撰寫時使用的語言。 yii\base\Application
$state integer 在請求處理生命週期期間,目前的應用程式狀態。 yii\base\Application
$timeZone string 此應用程式使用的時區。 yii\base\Application
$uniqueId string 模組的唯一 ID。 yii\base\Application
$urlManager yii\web\UrlManager 此應用程式的 URL 管理器。 yii\base\Application
$vendorPath string 儲存 vendor 檔案的目錄。 yii\base\Application
$version string 此模組的版本。 yii\base\Module
$view yii\base\View|yii\web\View 用於渲染各種視圖檔案的視圖應用程式元件。 yii\base\Application
$viewPath string 視圖檔案的根目錄。 yii\base\Module

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫未定義為類別方法的具名方法。 yii\base\Component
__clone() 在透過複製現有物件建立物件後,會呼叫此方法。 yii\base\Component
__construct() 建構子。 yii\base\Application
__get() Getter 魔術方法。 yii\di\ServiceLocator
__isset() 檢查屬性值是否為 null。 yii\di\ServiceLocator
__set() 設定元件屬性的值。 yii\base\Component
__unset() 將元件屬性設定為 null。 yii\base\Component
afterAction() 在此模組中的 action 執行後立即調用此方法。 yii\base\Module
attachBehavior() 將行為附加到此元件。 yii\base\Component
attachBehaviors() 將行為列表附加到元件。 yii\base\Component
beforeAction() 在此模組中的 action 執行前立即調用此方法。 yii\base\Module
behaviors() 傳回此元件應表現為的行為列表。 yii\base\Component
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\Component
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\Component
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
clear() 從定位器中移除元件。 yii\di\ServiceLocator
coreComponents() 傳回核心應用程式元件的配置。 yii\base\Application
createController() 根據給定的路徑建立控制器實例。 yii\base\Module
createControllerByID() 根據給定的控制器 ID 建立控制器。 yii\base\Module
detachBehavior() 從元件中分離行為。 yii\base\Component
detachBehaviors() 從元件中分離所有行為。 yii\base\Component
end() 終止應用程式。 yii\base\Application
ensureBehaviors() 確保在 behaviors() 中宣告的行為已附加到此元件。 yii\base\Component
get() 傳回具有指定 ID 的元件實例。 yii\base\Module
getAssetManager() 傳回 asset manager。 yii\base\Application
getAuthManager() 傳回此應用程式的 auth manager。 yii\base\Application
getBasePath() 傳回模組的根目錄。 yii\base\Module
getBehavior() 傳回具名的行為物件。 yii\base\Component
getBehaviors() 傳回附加到此元件的所有行為。 yii\base\Component
getCache() 傳回快取元件。 yii\base\Application
getComponents() 傳回元件定義或已載入的元件實例的列表。 yii\di\ServiceLocator
getControllerPath() 根據 $controllerNamespace 傳回包含控制器類別的目錄。 yii\base\Module
getDb() 傳回資料庫連線元件。 yii\base\Application
getErrorHandler() 傳回錯誤處理器元件。 yii\base\Application
getFormatter() 傳回格式器元件。 yii\base\Application
getI18n() 傳回國際化 (i18n) 元件。 yii\base\Application
getInstance() 傳回目前請求的此模組類別的實例。 yii\base\Module
getLayoutPath() 傳回包含此模組的版面配置視圖檔案的目錄。 yii\base\Module
getLog() 傳回日誌分派器元件。 yii\base\Application
getMailer() 傳回郵件器元件。 yii\base\Application
getModule() 檢索指定 ID 的子模組。 yii\base\Module
getModules() 傳回此模組中的子模組。 yii\base\Module
getRequest() 傳回請求元件。 yii\base\Application
getResponse() 傳回回應元件。 yii\base\Application
getRuntimePath() 傳回儲存執行階段檔案的目錄。 yii\base\Application
getSecurity() 傳回安全性元件。 yii\base\Application
getTimeZone() 傳回此應用程式使用的時區。 yii\base\Application
getUniqueId() 傳回一個 ID,在目前應用程式中的所有模組中唯一識別此模組。 yii\base\Application
getUrlManager() 傳回此應用程式的 URL 管理器。 yii\base\Application
getVendorPath() 傳回儲存 vendor 檔案的目錄。 yii\base\Application
getVersion() 傳回目前的模組版本。 yii\base\Module
getView() 傳回視圖物件。 yii\base\Application
getViewPath() 傳回包含此模組的視圖檔案的目錄。 yii\base\Module
handleRequest() 處理指定的請求。 yii\base\Application
has() 傳回一個值,指示定位器是否具有指定的元件定義或已實例化元件。 yii\base\Module
hasEventHandlers() 傳回一個值,指示是否有名稱事件附加任何處理常式。 yii\base\Component
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\Component
hasModule() 檢查指定 ID 的子模組是否存在。 yii\base\Module
hasProperty() 傳回一個值,指示是否為此元件定義屬性。 yii\base\Component
init() 初始化模組。 yii\base\Application
off() 從此元件中分離現有的事件處理常式。 yii\base\Component
on() 將事件處理常式附加到事件。 yii\base\Component
preInit() 預先初始化應用程式。 yii\base\Application
run() 執行應用程式。 yii\base\Application
runAction() 執行由路徑指定的控制器 action。 yii\base\Module
set() 向此定位器註冊元件定義。 yii\di\ServiceLocator
setAliases() 定義路徑別名。 yii\base\Module
setBasePath() 設定應用程式的根目錄和 @app 別名。 yii\base\Application
setComponents() 在此定位器中註冊一組元件定義。 yii\di\ServiceLocator
setContainer() 使用 $config 配置 Yii::$container yii\base\Application
setControllerPath() 設定包含控制器類別的目錄。 yii\base\Module
setInstance() 設定目前請求的此模組類別的實例。 yii\base\Module
setLayoutPath() 設定包含版面配置檔案的目錄。 yii\base\Module
setModule() 將子模組新增到此模組。 yii\base\Module
setModules() 在目前的模組中註冊子模組。 yii\base\Module
setRuntimePath() 設定儲存執行階段檔案的目錄。 yii\base\Application
setTimeZone() 設定此應用程式使用的時區。 yii\base\Application
setVendorPath() 設定儲存 vendor 檔案的目錄。 yii\base\Application
setVersion() 設定目前的模組版本。 yii\base\Module
setViewPath() 設定包含視圖檔案的目錄。 yii\base\Module
trigger() 觸發事件。 yii\base\Component

保護方法

隱藏繼承的方法

方法 描述 定義於
bootstrap() 初始化擴充功能並執行 bootstrap 元件。 yii\base\Application
defaultVersion() 傳回預設的模組版本。 yii\base\Module
registerErrorHandler() 將 errorHandler 元件註冊為 PHP 錯誤處理常式。 yii\base\Application

事件

隱藏繼承的事件

事件 類型 描述 定義於
EVENT_AFTER_ACTION yii\base\ActionEvent 在執行控制器 action 後引發的事件。 yii\base\Module
EVENT_AFTER_REQUEST yii\base\Event 在應用程式成功處理請求後(在回應傳送出去之前)引發的事件。 yii\base\Application
EVENT_BEFORE_ACTION yii\base\ActionEvent 在執行控制器 action 之前引發的事件。 yii\base\Module
EVENT_BEFORE_REQUEST yii\base\Event 在應用程式開始處理請求之前引發的事件。 yii\base\Application

常數

隱藏繼承的常數

常數 描述 定義於
STATE_AFTER_REQUEST 4 $state 使用的應用程式狀態:應用程式正在觸發 EVENT_AFTER_REQUEST yii\base\Application
STATE_BEFORE_REQUEST 2 $state 使用的應用程式狀態:應用程式正在觸發 EVENT_BEFORE_REQUEST yii\base\Application
STATE_BEGIN 0 $state 使用的應用程式狀態:應用程式剛啟動。 yii\base\Application
STATE_END 6 $state 使用的應用程式狀態:應用程式已結束。 yii\base\Application
STATE_HANDLING_REQUEST 3 $state 使用的應用程式狀態:應用程式正在處理請求。 yii\base\Application
STATE_INIT 1 $state 使用的應用程式狀態:應用程式正在初始化。 yii\base\Application
STATE_SENDING_RESPONSE 5 $state 使用的應用程式狀態:應用程式即將傳送回應。 yii\base\Application

屬性詳細資訊

隱藏繼承的屬性

$assetManager public property

資源管理器應用程式組件。

$authManager public property

權限管理器應用程式組件,如果未配置則為 null。

$basePath public property

應用程式的根目錄。

public string $basePath null
$bootstrap public property

在應用程式 啟動過程 中應執行的組件列表。

每個元件可以使用下列格式之一指定

  • 應用程式元件 ID,如透過 $components 指定。
  • 模組 ID,如透過 $modules 指定。
  • 類別名稱。
  • 配置陣列。
  • Closure

在引導啟動過程中,每個元件都將被實例化。如果元件類別實作了 yii\base\BootstrapInterface,則也會呼叫其 bootstrap() 方法。

public array $bootstrap = []
$cache public property

快取應用程式元件。如果未啟用元件,則為 Null。

$charset public property

應用程式目前使用的字元集。

public string $charset 'UTF-8'
$container public property

以名稱-值對形式給定的值。

public array $container null
$controller public property

目前活動的控制器實例

$controllerNamespace public property

控制器類別所在的命名空間。此命名空間將用於透過將其添加到控制器類別名稱的前面來載入控制器類別。預設命名空間為 app\controllers

請參閱 關於類別自動載入的指南 以了解更多詳細資訊。

public string $controllerNamespace 'app\controllers'
$db public property

資料庫連線。

public yii\db\Connection $db null
$errorHandler public property

錯誤處理器應用程式組件。

$extensions public property

已安裝的 Yii 擴充功能列表。每個陣列元素代表一個具有以下結構的單一擴充功能

[
    'name' => 'extension name',
    'version' => 'version number',
    'bootstrap' => 'BootstrapClassName',  // optional, may also be a configuration array
    'alias' => [
        '@alias1' => 'to/path1',
        '@alias2' => 'to/path2',
    ],
]

上面列出的 "bootstrap" 類別將在應用程式 引導啟動過程 中實例化。如果該類別實作了 yii\base\BootstrapInterface,則也會呼叫其 bootstrap() 方法。

如果未在應用程式配置中明確設定,則此屬性將使用 @vendor/yiisoft/extensions.php 的內容填充。

public array|null $extensions null
$formatter public property

格式化器應用程式組件。

$i18n public property

國際化應用程式組件。

public yii\i18n\I18N $i18n null
$language public property

預期供終端使用者使用的語言。建議您使用 IETF 語言標籤。例如,en 代表英文,而 en-US 代表英文(美國)。

另請參閱 $sourceLanguage

public string $language 'en-US'
$layout public property

應套用於此應用程式中視圖的版面配置。預設為 'main'。如果此值為 false,則將停用版面配置。

public string|boolean $layout 'main'
$loadedModules public property

依其類別名稱索引的已載入模組列表。

public array $loadedModules = []
$log public property

日誌分發器應用程式組件。

public yii\log\Dispatcher $log null
$mailer public property

郵件器應用程式組件。

$name public property

應用程式名稱。

public string $name 'My Application'
$request public property

請求元件。

$requestedAction public property

請求的 Action。如果為 null,則表示請求無法解析為 action。

$requestedParams public property

提供給請求的 action 的參數。

public array $requestedParams null
$requestedRoute public property

請求的路徑

public string $requestedRoute null
$response public property

回應元件。

$runtimePath public property

儲存執行階段檔案的目錄。預設為 $basePath 下的 "runtime" 子目錄。

public string $runtimePath null
$security public property

安全性應用程式元件。

$sourceLanguage public property

應用程式撰寫時使用的語言。這主要指的是訊息和視圖檔案撰寫時使用的語言。

另請參閱 $language

public string $sourceLanguage 'en-US'
$state public property

請求處理生命週期期間,應用程式目前的狀態。此屬性由應用程式管理。請勿修改此屬性。

public integer $state null
$timeZone public property

此應用程式使用的時區。

public string $timeZone null
$uniqueId public property

模組的唯一 ID。

public string $uniqueId null
$urlManager public property

此應用程式的 URL 管理器。

$vendorPath public property

儲存供應商檔案的目錄。預設為 $basePath 下的 "vendor" 目錄。

public string $vendorPath null
$view public property

用於渲染各種視圖檔案的視圖應用程式元件。

方法詳情

隱藏繼承的方法

__call() public method

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

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

此方法將檢查任何附加的行為是否具有指定名稱的方法,如果有的話將會執行它。

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

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

方法名稱

$params array

方法參數

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 ( $config [] )
$config array

將用於初始化物件屬性的名稱-值配對。請注意,配置必須同時包含 $id$basePath

throws yii\base\InvalidConfigException

如果缺少 $id$basePath 配置。

                public function __construct($config = [])
{
    Yii::$app = $this;
    static::setInstance($this);
    $this->state = self::STATE_BEGIN;
    $this->preInit($config);
    $this->registerErrorHandler($config);
    Component::__construct($config);
}

            
__get() public method

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

Getter 魔術方法。

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

public mixed __get ( $name )
$name string

組件或屬性名稱

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 string

屬性名稱或事件名稱

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 string

屬性名稱或事件名稱

$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 string

屬性名稱

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

定義於: yii\base\Module::afterAction()

在此模組中的 action 執行後立即調用此方法。

此方法將觸發 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 string

行為的名稱。

$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 array

要附加到組件的行為列表

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

            
beforeAction() public method

定義於: yii\base\Module::beforeAction()

在此模組中的 action 執行前立即調用此方法。

此方法將觸發 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 array

行為配置。

                public function behaviors()
{
    return [];
}

            
bootstrap() protected method

初始化擴充功能並執行 bootstrap 元件。

在應用程式完全配置後,init() 會呼叫此方法。如果您覆寫此方法,請確保您也呼叫父類別的實作。

protected void bootstrap ( )

                protected function bootstrap()
{
    if ($this->extensions === null) {
        $file = Yii::getAlias('@vendor/yiisoft/extensions.php');
        $this->extensions = is_file($file) ? include $file : [];
    }
    foreach ($this->extensions as $extension) {
        if (!empty($extension['alias'])) {
            foreach ($extension['alias'] as $name => $path) {
                Yii::setAlias($name, $path);
            }
        }
        if (isset($extension['bootstrap'])) {
            $component = Yii::createObject($extension['bootstrap']);
            if ($component instanceof BootstrapInterface) {
                Yii::debug('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
                $component->bootstrap($this);
            } else {
                Yii::debug('Bootstrap with ' . get_class($component), __METHOD__);
            }
        }
    }
    foreach ($this->bootstrap as $mixed) {
        $component = null;
        if ($mixed instanceof \Closure) {
            Yii::debug('Bootstrap with Closure', __METHOD__);
            if (!$component = call_user_func($mixed, $this)) {
                continue;
            }
        } elseif (is_string($mixed)) {
            if ($this->has($mixed)) {
                $component = $this->get($mixed);
            } elseif ($this->hasModule($mixed)) {
                $component = $this->getModule($mixed);
            } elseif (strpos($mixed, '\\') === false) {
                throw new InvalidConfigException("Unknown bootstrapping component ID: $mixed");
            }
        }
        if (!isset($component)) {
            $component = Yii::createObject($mixed);
        }
        if ($component instanceof BootstrapInterface) {
            Yii::debug('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
            $component->bootstrap($this);
        } else {
            Yii::debug('Bootstrap with ' . get_class($component), __METHOD__);
        }
    }
}

            
canGetProperty() public method

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

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

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

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

另請參閱 canSetProperty()

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

屬性名稱

$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 string

屬性名稱

$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 string

此類別的完整限定名稱。

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

            
clear() public method

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

從定位器中移除元件。

public void clear ( $id )
$id string

組件 ID

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

            
coreComponents() public method

傳回核心應用程式元件的配置。

另請參閱 set()

public array coreComponents ( )

                public function coreComponents()
{
    $components = [
        'log' => ['class' => 'yii\log\Dispatcher'],
        'view' => ['class' => 'yii\web\View'],
        'formatter' => ['class' => 'yii\i18n\Formatter'],
        'i18n' => ['class' => 'yii\i18n\I18N'],
        'urlManager' => ['class' => 'yii\web\UrlManager'],
        'assetManager' => ['class' => 'yii\web\AssetManager'],
        'security' => ['class' => 'yii\base\Security'],
    ];
    if (class_exists('yii\swiftmailer\Mailer')) {
        $components['mailer'] = ['class' => 'yii\swiftmailer\Mailer'];
    }
    return $components;
}

            
createController() public method

定義於: yii\base\Module::createController()

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

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

  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 string

由模組、控制器和動作 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

定義於: yii\base\Module::createControllerByID()

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

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

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

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

控制器 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)

定義於: yii\base\Module::defaultVersion()

傳回預設的模組版本。

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

protected string defaultVersion ( )
return string

此模組的版本。

                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 string

行為的名稱。

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

            
end() public method

終止應用程式。

此方法取代 exit() 函數,確保應用程式生命週期在終止應用程式之前完成。

public void end ( $status 0, $response null )
$status integer

退出狀態(值 0 表示正常退出,其他值表示異常退出)。

$response yii\base\Response|null

要發送的回應。如果未設定,將使用預設應用程式 $response 組件。

throws yii\base\ExitException

如果應用程式處於測試模式

                public function end($status = 0, $response = null)
{
    if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
        $this->state = self::STATE_AFTER_REQUEST;
        $this->trigger(self::EVENT_AFTER_REQUEST);
    }
    if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
        $this->state = self::STATE_END;
        $response = $response ?: $this->getResponse();
        $response->send();
    }
    if (YII_ENV_TEST) {
        throw new ExitException($status);
    }
    exit($status);
}

            
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

定義於: yii\base\Module::get()

傳回具有指定 ID 的元件實例。

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

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

組件 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;
}

            
getAssetManager() public method

傳回 asset manager。

public yii\web\AssetManager getAssetManager ( )
return yii\web\AssetManager

資源管理器應用程式組件。

                public function getAssetManager()
{
    return $this->get('assetManager');
}

            
getAuthManager() public method

傳回此應用程式的 auth manager。

public yii\rbac\ManagerInterface|null getAuthManager ( )
return yii\rbac\ManagerInterface|null

權限管理器應用程式組件,如果未配置則為 null。

                public function getAuthManager()
{
    return $this->get('authManager', false);
}

            
getBasePath() public method

定義於: yii\base\Module::getBasePath()

傳回模組的根目錄。

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

public string getBasePath ( )
return string

模組的根目錄。

                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 string

行為名稱

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

            
getCache() public method

傳回快取元件。

public yii\caching\CacheInterface|null getCache ( )
return yii\caching\CacheInterface|null

快取應用程式元件。如果未啟用元件,則為 Null。

                public function getCache()
{
    return $this->get('cache', false);
}

            
getComponents() public method

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

傳回元件定義或已載入的元件實例的列表。

public array getComponents ( $returnDefinitions true )
$returnDefinitions boolean

是否返回組件定義而不是已載入的組件實例。

return array

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

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

            
getControllerPath() public method

定義於: yii\base\Module::getControllerPath()

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

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

public string getControllerPath ( )
return string

包含控制器類別的目錄。

throws yii\base\InvalidArgumentException

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

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

            
getDb() public method

傳回資料庫連線元件。

public yii\db\Connection getDb ( )
return yii\db\Connection

資料庫連線。

                public function getDb()
{
    return $this->get('db');
}

            
getErrorHandler() public method

傳回錯誤處理器元件。

public yii\web\ErrorHandler|yii\console\ErrorHandler getErrorHandler ( )
return yii\web\ErrorHandler|yii\console\ErrorHandler

錯誤處理器應用程式組件。

                public function getErrorHandler()
{
    return $this->get('errorHandler');
}

            
getFormatter() public method

傳回格式器元件。

public yii\i18n\Formatter getFormatter ( )
return yii\i18n\Formatter

格式化器應用程式組件。

                public function getFormatter()
{
    return $this->get('formatter');
}

            
getI18n() public method

傳回國際化 (i18n) 元件。

public yii\i18n\I18N getI18n ( )
return yii\i18n\I18N

國際化應用程式組件。

                public function getI18n()
{
    return $this->get('i18n');
}

            
getInstance() public static method

定義於: yii\base\Module::getInstance()

傳回目前請求的此模組類別的實例。

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

public static static|null getInstance ( )
return static|null

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

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

            
getLayoutPath() public method

定義於: yii\base\Module::getLayoutPath()

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

public string getLayoutPath ( )
return string

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

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

            
getLog() public method

傳回日誌分派器元件。

public yii\log\Dispatcher getLog ( )
return yii\log\Dispatcher

日誌分發器應用程式組件。

                public function getLog()
{
    return $this->get('log');
}

            
getMailer() public method

傳回郵件器元件。

public yii\mail\MailerInterface getMailer ( )
return yii\mail\MailerInterface

郵件器應用程式組件。

throws yii\base\InvalidConfigException

如果此組件未設定。

                public function getMailer()
{
    return $this->get('mailer');
}

            
getModule() public method

定義於: yii\base\Module::getModule()

檢索指定 ID 的子模組。

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

另請參閱 hasModule()

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

模組 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

定義於: yii\base\Module::getModules()

傳回此模組中的子模組。

public array getModules ( $loadedOnly false )
$loadedOnly boolean

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

return array

模組(以它們的 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;
}

            
getRequest() public method

傳回請求元件。

public yii\web\Request|yii\console\Request getRequest ( )
return yii\web\Request|yii\console\Request

請求元件。

                public function getRequest()
{
    return $this->get('request');
}

            
getResponse() public method

傳回回應元件。

public yii\web\Response|yii\console\Response getResponse ( )
return yii\web\Response|yii\console\Response

回應元件。

                public function getResponse()
{
    return $this->get('response');
}

            
getRuntimePath() public method

傳回儲存執行階段檔案的目錄。

public string getRuntimePath ( )
return string

儲存執行階段檔案的目錄。預設為 $basePath 下的 "runtime" 子目錄。

                public function getRuntimePath()
{
    if ($this->_runtimePath === null) {
        $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
    }
    return $this->_runtimePath;
}

            
getSecurity() public method

傳回安全性元件。

public yii\base\Security getSecurity ( )
return yii\base\Security

安全性應用程式元件。

                public function getSecurity()
{
    return $this->get('security');
}

            
getTimeZone() public method

傳回此應用程式使用的時區。

這是 PHP 函數 date_default_timezone_get() 的簡單包裝器。 如果時區未在 php.ini 或應用程式組態中設定,則預設會設定為 UTC。

另請參閱 https://php.dev.org.tw/manual/en/function.date-default-timezone-get.php

public string getTimeZone ( )
return string

此應用程式使用的時區。

                public function getTimeZone()
{
    return date_default_timezone_get();
}

            
getUniqueId() public method

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

由於這是應用程式實例,因此它將始終返回空字串。

public string getUniqueId ( )
return string

模組的唯一 ID。

                public function getUniqueId()
{
    return '';
}

            
getUrlManager() public method

傳回此應用程式的 URL 管理器。

public yii\web\UrlManager getUrlManager ( )
return yii\web\UrlManager

此應用程式的 URL 管理器。

                public function getUrlManager()
{
    return $this->get('urlManager');
}

            
getVendorPath() public method

傳回儲存 vendor 檔案的目錄。

public string getVendorPath ( )
return string

儲存供應商檔案的目錄。預設為 $basePath 下的 "vendor" 目錄。

                public function getVendorPath()
{
    if ($this->_vendorPath === null) {
        $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
    }
    return $this->_vendorPath;
}

            
getVersion() public method (available since version 2.0.11)

定義於: yii\base\Module::getVersion()

傳回目前的模組版本。

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

public string getVersion ( )
return string

此模組的版本。

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

            
getView() public method

傳回視圖物件。

public yii\base\View|yii\web\View getView ( )
return yii\base\View|yii\web\View

用於渲染各種視圖檔案的視圖應用程式元件。

                public function getView()
{
    return $this->get('view');
}

            
getViewPath() public method

定義於: yii\base\Module::getViewPath()

傳回包含此模組的視圖檔案的目錄。

public string getViewPath ( )
return string

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

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

            
handleRequest() public abstract method

處理指定的請求。

此方法應返回 yii\base\Response 或其子類別的實例,該實例表示請求的處理結果。

public abstract yii\base\Response handleRequest ( $request )
$request yii\base\Request

要處理的請求

return yii\base\Response

產生的回應

                abstract public function handleRequest($request);

            
has() public method

定義於: yii\base\Module::has()

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

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

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

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

組件 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 string

事件名稱

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 string

屬性名稱

$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

定義於: yii\base\Module::hasModule()

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

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

public boolean hasModule ( $id )
$id string

模組 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 string

屬性名稱

$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()
{
    $this->state = self::STATE_INIT;
    $this->bootstrap();
}

            
off() public method

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

從此元件中分離現有的事件處理常式。

此方法與 on() 相反。

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

另請參閱 on()

public boolean off ( $name, $handler null )
$name string

事件名稱

$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 string

事件名稱

$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]);
    }
}

            
preInit() public method

預先初始化應用程式。

此方法在應用程式建構子的開頭調用。它初始化幾個重要的應用程式屬性。如果您覆寫此方法,請確保您調用父類別的實作。

public void preInit ( &$config )
$config array

應用程式配置

throws yii\base\InvalidConfigException

如果缺少 $id$basePath 配置。

                public function preInit(&$config)
{
    if (!isset($config['id'])) {
        throw new InvalidConfigException('The "id" configuration for the Application is required.');
    }
    if (isset($config['basePath'])) {
        $this->setBasePath($config['basePath']);
        unset($config['basePath']);
    } else {
        throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
    }
    if (isset($config['vendorPath'])) {
        $this->setVendorPath($config['vendorPath']);
        unset($config['vendorPath']);
    } else {
        // set "@vendor"
        $this->getVendorPath();
    }
    if (isset($config['runtimePath'])) {
        $this->setRuntimePath($config['runtimePath']);
        unset($config['runtimePath']);
    } else {
        // set "@runtime"
        $this->getRuntimePath();
    }
    if (isset($config['timeZone'])) {
        $this->setTimeZone($config['timeZone']);
        unset($config['timeZone']);
    } elseif (!ini_get('date.timezone')) {
        $this->setTimeZone('UTC');
    }
    if (isset($config['container'])) {
        $this->setContainer($config['container']);
        unset($config['container']);
    }
    // merge core components with custom components
    foreach ($this->coreComponents() as $id => $component) {
        if (!isset($config['components'][$id])) {
            $config['components'][$id] = $component;
        } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
            $config['components'][$id]['class'] = $component['class'];
        }
    }
}

            
registerErrorHandler() protected method

將 errorHandler 元件註冊為 PHP 錯誤處理常式。

protected void registerErrorHandler ( &$config )
$config array

應用程式配置

                protected function registerErrorHandler(&$config)
{
    if (YII_ENABLE_ERROR_HANDLER) {
        if (!isset($config['components']['errorHandler']['class'])) {
            echo "Error: no errorHandler component is configured.\n";
            exit(1);
        }
        $this->set('errorHandler', $config['components']['errorHandler']);
        unset($config['components']['errorHandler']);
        $this->getErrorHandler()->register();
    }
}

            
run() public method

執行應用程式。

這是應用程式的主要入口點。

public integer run ( )
return integer

退出狀態 (0 表示正常,非零值表示異常)

                public function run()
{
    try {
        $this->state = self::STATE_BEFORE_REQUEST;
        $this->trigger(self::EVENT_BEFORE_REQUEST);
        $this->state = self::STATE_HANDLING_REQUEST;
        $response = $this->handleRequest($this->getRequest());
        $this->state = self::STATE_AFTER_REQUEST;
        $this->trigger(self::EVENT_AFTER_REQUEST);
        $this->state = self::STATE_SENDING_RESPONSE;
        $response->send();
        $this->state = self::STATE_END;
        return $response->exitStatus;
    } catch (ExitException $e) {
        $this->end($e->statusCode, isset($response) ? $response : null);
        return $e->statusCode;
    }
}

            
runAction() public method

定義於: yii\base\Module::runAction()

執行由路徑指定的控制器 action。

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

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

指定動作的路由。

$params array

要傳遞給動作的參數

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 string

組件 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\base\Module::setAliases()

定義路徑別名。

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

public void setAliases ( $aliases )
$aliases array

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

[
    '@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

設定應用程式的根目錄和 @app 別名。

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

public void setBasePath ( $path )
$path string

應用程式的根目錄。

throws yii\base\InvalidArgumentException

如果目錄不存在。

                public function setBasePath($path)
{
    parent::setBasePath($path);
    Yii::setAlias('@app', $this->getBasePath());
}

            
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 array

元件定義或實例

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

            
setContainer() public method (自 2.0.11 版本起可用)

使用 $config 配置 Yii::$container

public void setContainer ( $config )
$config array

以名稱-值對形式給定的值

                public function setContainer($config)
{
    Yii::configure(Yii::$container, $config);
}

            
setControllerPath() public method (自 2.0.44 版本起可用)

定義於: yii\base\Module::setControllerPath()

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

public void setControllerPath ( $path )
$path string

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

throws yii\base\InvalidArgumentException

如果目錄無效。

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

            
setInstance() public static method

定義於: yii\base\Module::setInstance()

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

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

定義於: yii\base\Module::setLayoutPath()

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

public void setLayoutPath ( $path )
$path string

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

throws yii\base\InvalidArgumentException

如果目錄無效

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

            
setModule() public method

定義於: yii\base\Module::setModule()

將子模組新增到此模組。

public void setModule ( $id, $module )
$id string

模組 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

定義於: yii\base\Module::setModules()

在目前的模組中註冊子模組。

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

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

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

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

模組 (id => 模組配置或實例)。

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

            
setRuntimePath() public method

設定儲存執行階段檔案的目錄。

public void setRuntimePath ( $path )
$path string

儲存執行階段檔案的目錄。

                public function setRuntimePath($path)
{
    $this->_runtimePath = Yii::getAlias($path);
    Yii::setAlias('@runtime', $this->_runtimePath);
}

            
setTimeZone() public method

設定此應用程式使用的時區。

這是 PHP 函數 date_default_timezone_set() 的簡單包裝器。有關可用的時區,請參閱 php 手冊

另請參閱 https://php.dev.org.tw/manual/en/function.date-default-timezone-set.php

public void setTimeZone ( $value )
$value string

此應用程式使用的時區。

                public function setTimeZone($value)
{
    date_default_timezone_set($value);
}

            
setVendorPath() public method

設定儲存 vendor 檔案的目錄。

public void setVendorPath ( $path )
$path string

儲存 vendor 檔案的目錄。

                public function setVendorPath($path)
{
    $this->_vendorPath = Yii::getAlias($path);
    Yii::setAlias('@vendor', $this->_vendorPath);
    Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
    Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
}

            
setVersion() public method (自 2.0.11 版本起可用)

定義於: yii\base\Module::setVersion()

設定目前的模組版本。

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

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

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

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

            
setViewPath() public method

定義於: yii\base\Module::setViewPath()

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

public void setViewPath ( $path )
$path string

視圖檔案的根目錄。

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 string

事件名稱

$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_REQUEST event of type yii\base\Event

在應用程式成功處理請求後(在回應傳送出去之前)引發的事件。

EVENT_BEFORE_REQUEST event of type yii\base\Event

在應用程式開始處理請求之前引發的事件。