0 追蹤者

類別 yii\console\Application

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

Application 代表一個命令列應用程式。

Application 繼承自 yii\base\Application,並提供特定於命令列請求的功能。 特別是,它通過基於命令的方法處理命令列請求

  • 一個命令列應用程式由一個或多個可能的用戶命令組成;
  • 每個用戶命令都實現為一個擴展 yii\console\Controller 的類別;
  • 用戶在命令列上指定要運行的命令;
  • 命令使用指定的參數處理用戶請求。

命令類別應位於 $controllerNamespace 指定的命名空間下。它們的命名應遵循與控制器相同的命名約定。 例如,help 命令是使用 HelpController 類別實現的。

要運行命令列應用程式,請在命令列中輸入以下內容

yii <route> [--param1=value1 --param2 ...]

其中 <route> 指的是控制器路由,格式為 ModuleID/ControllerID/ActionID (例如 sitemap/create),而 param1param2 指的是一組命名參數,這些參數將用於初始化控制器操作 (例如 --since=0 指定一個 since 參數,其值為 0,並且將相應的 $since 參數傳遞給操作方法)。

預設提供 help 命令,它列出可用的命令並顯示其用法。 要使用此命令,只需輸入

yii help

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$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\console\Controller 目前活動的控制器實例 yii\console\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\console\Application
$enableCoreCommands boolean 是否啟用核心框架提供的命令。 yii\console\Application
$errorHandler yii\console\ErrorHandler 錯誤處理器應用程式組件。 yii\console\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 自訂模組參數 (name => value)。 yii\base\Module
$request yii\console\Request 請求組件。 yii\console\Application
$requestedAction yii\base\Action|null 請求的操作。 yii\base\Application
$requestedParams array 提供給請求操作的參數。 yii\base\Application
$requestedRoute string 請求的路由 yii\base\Application
$response yii\console\Response 響應組件。 yii\console\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\console\Application
__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
coreCommands() 傳回內建命令的配置。 yii\console\Application
coreComponents() 傳回核心應用程式組件的配置。 yii\console\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() 傳回資源管理器。 yii\base\Application
getAuthManager() 傳回此應用程式的授權管理器。 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\console\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\console\Application
getResponse() 傳回應答組件。 yii\console\Application
getRuntimePath() 傳回儲存執行階段檔案的目錄。 yii\base\Application
getSecurity() 傳回安全性組件。 yii\base\Application
getTimeZone() 傳回此應用程式使用的時區。 yii\base\Application
getUniqueId() 傳回在目前應用程式中的所有模組中,唯一識別此模組的 ID。 yii\base\Application
getUrlManager() 傳回此應用程式的 URL 管理器。 yii\base\Application
getVendorPath() 傳回儲存供應商檔案的目錄。 yii\base\Application
getVersion() 傳回目前的模組版本。 yii\base\Module
getView() 傳回視圖物件。 yii\base\Application
getViewPath() 傳回包含此模組視圖檔案的目錄。 yii\base\Module
handleRequest() 處理指定的請求。 yii\console\Application
has() 傳回一個值,指示定位器是否具有指定的組件定義,或是否已實例化組件。 yii\base\Module
hasEventHandlers() 傳回一個值,指示是否有名稱事件附加任何處理常式。 yii\base\Component
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\Component
hasModule() 檢查是否已存在指定 ID 的子模組。 yii\base\Module
hasProperty() 傳回一個值,指示是否已為此組件定義屬性。 yii\base\Component
init() 初始化應用程式。 yii\console\Application
off() 從此組件分離現有的事件處理常式。 yii\base\Component
on() 將事件處理常式附加到事件。 yii\base\Component
preInit() 預先初始化應用程式。 yii\base\Application
run() 執行應用程式。 yii\base\Application
runAction() 執行路由指定的控制器動作。 yii\console\Application
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() 設定儲存供應商檔案的目錄。 yii\base\Application
setVersion() 設定目前的模組版本。 yii\base\Module
setViewPath() 設定包含視圖檔案的目錄。 yii\base\Module
trigger() 觸發事件。 yii\base\Component

受保護方法

隱藏繼承的方法

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

事件

隱藏繼承的事件

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

常數

隱藏繼承的常數

常數 描述 定義於
OPTION_APPCONFIG 'appconfig' 用於指定應用程式組態檔路徑的選項名稱。 yii\console\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

屬性詳細資訊

隱藏繼承的屬性

$controller 公共屬性

目前活動的控制器實例

$defaultRoute 公共屬性

此應用程式的預設路由。預設為 'help',表示 help 命令。

public string $defaultRoute 'help'
$enableCoreCommands 公共屬性

是否啟用核心框架提供的命令。預設為 true。

$errorHandler 公共屬性

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

$request 公共屬性

請求組件。

$response 公共屬性

響應組件。

方法詳細資訊

隱藏繼承的方法

__call() 公共方法

定義於: 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() 公共方法

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

在透過複製現有物件建立物件後,會呼叫此方法。

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

public void __clone ( )

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

            
__construct() 公共方法

建構子。

public void __construct ( $config = [] )
$config array

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

throws yii\base\InvalidConfigException

如果缺少 $id$basePath 組態,則會擲回例外。

                public function __construct($config = [])
{
    $config = $this->loadConfig($config);
    parent::__construct($config);
}

            
__get() 公共方法

定義於: 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() 公共方法

定義於: 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() 公共方法

定義於: 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() 公共方法

定義於: 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() 公共方法

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

此方法會在模組中的動作執行完成後立即調用。

此方法將觸發 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() 公共方法

定義於: 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() 公共方法

定義於: 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() 公共方法

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

此方法會在模組中的動作執行之前立即調用。

此方法將觸發 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() 公共方法

定義於: 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() 受保護方法

定義於: yii\base\Application::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() 公共方法

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

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

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

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

另請參閱 canSetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為 (behaviors) 的屬性視為此組件的屬性

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 時);
  • 附加的行為 (behavior) 具有給定名稱的可寫入屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canGetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為 (behaviors) 的屬性視為此組件的屬性

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

            
coreCommands() public method

傳回內建命令的配置。

public array coreCommands ( )
return array

內建命令的設定。

                public function coreCommands()
{
    return [
        'asset' => 'yii\console\controllers\AssetController',
        'cache' => 'yii\console\controllers\CacheController',
        'fixture' => 'yii\console\controllers\FixtureController',
        'help' => 'yii\console\controllers\HelpController',
        'message' => 'yii\console\controllers\MessageController',
        'migrate' => 'yii\console\controllers\MigrateController',
        'serve' => 'yii\console\controllers\ServeController',
    ];
}

            
coreComponents() public method

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

public array coreComponents ( )

                public function coreComponents()
{
    return array_merge(parent::coreComponents(), [
        'request' => ['class' => 'yii\console\Request'],
        'response' => ['class' => 'yii\console\Response'],
        'errorHandler' => ['class' => 'yii\console\ErrorHandler'],
    ]);
}

            
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 (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()

從組件中分離行為。

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

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

行為 (behavior) 的名稱。

return yii\base\Behavior|null

已分離的行為 (behavior)。如果行為不存在,則為 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

定義於: yii\base\Application::end()

終止應用程式。

此方法取代了 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 之前未在定位器 (locator) 中註冊,是否拋出例外。

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

定義於: yii\base\Application::getAssetManager()

傳回資源管理器。

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

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

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

            
getAuthManager() public method

定義於: yii\base\Application::getAuthManager()

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

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

行為 (behavior) 名稱

return yii\base\Behavior|null

行為 (behavior) 物件,如果行為不存在,則為 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[]

附加到此組件的行為 (behavior) 列表

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

            
getCache() public method

定義於: yii\base\Application::getCache()

傳回快取組件。

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

定義於: yii\base\Application::getDb()

傳回資料庫連線組件。

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

資料庫連線。

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

            
getErrorHandler() public method

傳回錯誤處理器組件。

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

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

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

            
getFormatter() public method

定義於: yii\base\Application::getFormatter()

傳回格式器組件。

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

格式化器應用程式組件。

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

            
getI18n() public method

定義於: yii\base\Application::getI18n()

傳回國際化 (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 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

定義於: 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

定義於: yii\base\Application::getLog()

傳回記錄分派器組件。

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

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

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

            
getMailer() public method

定義於: yii\base\Application::getMailer()

傳回郵件器組件。

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\console\Request getRequest ( )
return yii\console\Request

請求組件。

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

            
getResponse() public method

傳回應答組件。

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

響應組件。

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

            
getRuntimePath() public method

Defined in: yii\base\Application::getRuntimePath()

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

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

Defined in: yii\base\Application::getSecurity()

傳回安全性組件。

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

安全應用程式組件。

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

            
getTimeZone() public method

Defined in: yii\base\Application::getTimeZone()

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

這是 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

Defined in: yii\base\Application::getUniqueId()

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

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

public string getUniqueId ( )
return string

模組的唯一 ID。

                public function getUniqueId()
{
    return '';
}

            
getUrlManager() public method

Defined in: yii\base\Application::getUrlManager()

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

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

此應用程式的 URL 管理器。

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

            
getVendorPath() public method

Defined in: yii\base\Application::getVendorPath()

傳回儲存供應商檔案的目錄。

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)

Defined in: 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

Defined in: yii\base\Application::getView()

傳回視圖物件。

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

Defined in: 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 method

處理指定的請求。

public yii\console\Response handleRequest ( $request )
$request yii\console\Request

要處理的請求

return yii\console\Response

產生的回應

                public function handleRequest($request)
{
    list($route, $params) = $request->resolve();
    $this->requestedRoute = $route;
    $result = $this->runAction($route, $params);
    if ($result instanceof Response) {
        return $result;
    }
    $response = $this->getResponse();
    $response->exitStatus = $result;
    return $response;
}

            
has() public method

Defined in: 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

Defined in: 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

Defined in: 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

Defined in: 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

Defined in: yii\base\Component::hasProperty()

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

如果滿足以下條件,則定義屬性:

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

參見

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為 (behaviors) 的屬性視為此組件的屬性

return boolean

屬性是否已定義

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

            
init() public method

初始化應用程式。

public void init ( )

                public function init()
{
    parent::init();
    if ($this->enableCoreCommands) {
        foreach ($this->coreCommands() as $id => $command) {
            if (!isset($this->controllerMap[$id])) {
                $this->controllerMap[$id] = $command;
            }
        }
    }
    // ensure we have the 'help' command so that we can list the available commands
    if (!isset($this->controllerMap['help'])) {
        $this->controllerMap['help'] = 'yii\console\controllers\HelpController';
    }
}

            
loadConfig() protected method

載入配置。

此方法將檢查是否指定了命令列選項 OPTION_APPCONFIG。如果是,則將載入相應的檔案作為應用程式配置。否則,將返回作為參數提供的配置。

protected array loadConfig ( $config )
$config array

在建構子中提供的配置。

return array

應用程式要使用的實際配置。

                protected function loadConfig($config)
{
    if (!empty($_SERVER['argv'])) {
        $option = '--' . self::OPTION_APPCONFIG . '=';
        foreach ($_SERVER['argv'] as $param) {
            if (strpos($param, $option) !== false) {
                $path = substr($param, strlen($option));
                if (!empty($path) && is_file($file = Yii::getAlias($path))) {
                    return require $file;
                }
                exit("The configuration file does not exist: $path\n");
            }
        }
    }
    return $config;
}

            
off() public method

Defined in: 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

Defined in: 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

Defined in: yii\base\Application::preInit()

預先初始化應用程式。

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

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

Defined in: yii\base\Application::registerErrorHandler()

將 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

Defined in: yii\base\Application::run()

執行應用程式。

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

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\console\Controller::runAction() 以使用給定的參數執行動作。如果路由為空,則此方法將使用 $defaultRoute

例如,要執行 public function actionTest($a, $b) 假設控制器具有選項,應使用以下程式碼

\Yii::$app->runAction('controller/test', ['option' => 'value', $a, $b]);
public integer|yii\console\Response|null runAction ( $route, $params = [] )
$route string

指定動作的路由。

$params array

要傳遞給動作的參數

return integer|yii\console\Response|null

動作的結果。這可以是退出代碼或 Response 物件。退出代碼 0 表示正常,其他值表示異常。退出代碼為 null 被視為 0

throws yii\console\Exception

如果路由無效

                public function runAction($route, $params = [])
{
    try {
        $res = parent::runAction($route, $params);
        return is_object($res) ? $res : (int) $res;
    } catch (InvalidRouteException $e) {
        throw new UnknownCommandException($route, $this, 0, $e);
    }
}

            
set() public method

Defined in: 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

Defined in: 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

Defined in: yii\base\Application::setBasePath()

設定應用程式的根目錄和 @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 方法

定義於: 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 方法 (自版本 2.0.11 起可用)

定義於: yii\base\Application::setContainer()

使用 $config 配置 Yii::$container

public void setContainer ( $config )
$config array

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

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

            
setControllerPath() public 方法 (自版本 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 靜態方法

定義於: 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 方法

定義於: 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 方法

定義於: 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 方法

定義於: 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 方法

定義於: yii\base\Application::setRuntimePath()

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

public void setRuntimePath ( $path )
$path string

儲存運行時檔案的目錄。

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

            
setTimeZone() public 方法

定義於: yii\base\Application::setTimeZone()

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

這是 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 方法

定義於: yii\base\Application::setVendorPath()

設定儲存供應商檔案的目錄。

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 方法 (自版本 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 方法

定義於: 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 方法

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