抽象類別 yii\base\Application
Application 是所有應用程式類別的基底類別。
關於 Application 的更多細節與使用資訊,請參閱 應用程式指南文章。
公共屬性
公開方法
保護方法
方法 | 描述 | 定義於 |
---|---|---|
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 |
屬性詳細資訊
權限管理器應用程式組件,如果未配置則為 null。
在應用程式 啟動過程 中應執行的組件列表。
每個元件可以使用下列格式之一指定
- 應用程式元件 ID,如透過 $components 指定。
- 模組 ID,如透過 $modules 指定。
- 類別名稱。
- 配置陣列。
- Closure
在引導啟動過程中,每個元件都將被實例化。如果元件類別實作了 yii\base\BootstrapInterface,則也會呼叫其 bootstrap() 方法。
快取應用程式元件。如果未啟用元件,則為 Null。
控制器類別所在的命名空間。此命名空間將用於透過將其添加到控制器類別名稱的前面來載入控制器類別。預設命名空間為 app\controllers
。
請參閱 關於類別自動載入的指南 以了解更多詳細資訊。
錯誤處理器應用程式組件。
已安裝的 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
的內容填充。
預期供終端使用者使用的語言。建議您使用 IETF 語言標籤。例如,en
代表英文,而 en-US
代表英文(美國)。
另請參閱 $sourceLanguage。
應套用於此應用程式中視圖的版面配置。預設為 'main'。如果此值為 false,則將停用版面配置。
請求的 Action。如果為 null,則表示請求無法解析為 action。
儲存執行階段檔案的目錄。預設為 $basePath 下的 "runtime" 子目錄。
應用程式撰寫時使用的語言。這主要指的是訊息和視圖檔案撰寫時使用的語言。
另請參閱 $language。
儲存供應商檔案的目錄。預設為 $basePath 下的 "vendor" 目錄。
方法詳情
定義於: 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()");
}
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
建構子。
public void __construct ( $config = [] ) | ||
$config | array | |
throws | yii\base\InvalidConfigException |
---|
public function __construct($config = [])
{
Yii::$app = $this;
static::setInstance($this);
$this->state = self::STATE_BEGIN;
$this->preInit($config);
$this->registerErrorHandler($config);
Component::__construct($config);
}
public mixed __get ( $name ) | ||
$name | string |
組件或屬性名稱 |
return | mixed |
指定的屬性值 |
---|
public function __get($name)
{
if ($this->has($name)) {
return $this->get($name);
}
return parent::__get($name);
}
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
屬性值是否為 null |
---|
public function __isset($name)
{
if ($this->has($name)) {
return true;
}
return parent::__isset($name);
}
定義於: 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);
}
定義於: yii\base\Component::__unset()
將元件屬性設定為 null。
此方法將依以下順序檢查並採取相應措施
- 由 setter 定義的屬性:將屬性值設定為 null
- 行為的屬性:將屬性值設定為 null
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($component->property)
時會被隱式呼叫。
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);
}
定義於: 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;
}
定義於: 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);
}
定義於: 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);
}
}
定義於: 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;
}
定義於: 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 元件。
在應用程式完全配置後,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__);
}
}
}
定義於: 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;
}
定義於: 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;
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
定義於: yii\di\ServiceLocator::clear()
從定位器中移除元件。
public void clear ( $id ) | ||
$id | string |
組件 ID |
public function clear($id)
{
unset($this->_definitions[$id], $this->_components[$id]);
}
傳回核心應用程式元件的配置。
另請參閱 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;
}
定義於: yii\base\Module::createController()
根據給定的路徑建立控制器實例。
路由應相對於此模組。此方法實作以下演算法來解析給定的路由
- 如果路由為空,則使用 $defaultRoute;
- 如果在 $controllerMap 中找到路由的第一個區段,則根據 $controllerMap 中找到的相應配置建立控制器;
- 如果路由的第一個區段是在 $modules 中宣告的有效模組 ID,則使用路由的其餘部分呼叫模組的
createController()
; - 給定的路由格式為
abc/def/xyz
。嘗試 控制器命名空間 中的abc\DefController
或abc\def\XyzController
類別。
如果以上任何步驟解析為控制器,則會與路由的其餘部分一起返回,這些部分將被視為動作 ID。否則,將返回 false
。
public array|boolean createController ( $route ) | ||
$route | string |
由模組、控制器和動作 ID 組成的路由。 |
return | array|boolean |
如果控制器建立成功,它將與請求的動作 ID 一起返回。否則將返回 |
---|---|---|
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];
}
定義於: 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 無效,則為 |
---|---|---|
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;
}
protected string defaultVersion ( ) | ||
return | string |
此模組的版本。 |
---|
protected function defaultVersion()
{
if ($this->module === null) {
return '1.0';
}
return $this->module->getVersion();
}
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;
}
定義於: yii\base\Component::detachBehaviors()
從元件中分離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
終止應用程式。
此方法取代 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);
}
定義於: 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);
}
}
}
public object|null get ( $id, $throwException = true ) | ||
$id | string |
組件 ID(例如 |
$throwException | boolean |
是否在 |
return | object|null |
指定 ID 的組件。如果 |
---|---|---|
throws | yii\base\InvalidConfigException |
如果 |
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;
}
傳回 asset manager。
public yii\web\AssetManager getAssetManager ( ) | ||
return | yii\web\AssetManager |
資源管理器應用程式組件。 |
---|
public function getAssetManager()
{
return $this->get('assetManager');
}
傳回此應用程式的 auth manager。
public yii\rbac\ManagerInterface|null getAuthManager ( ) | ||
return | yii\rbac\ManagerInterface|null |
權限管理器應用程式組件,如果未配置則為 null。 |
---|
public function getAuthManager()
{
return $this->get('authManager', false);
}
public string getBasePath ( ) | ||
return | string |
模組的根目錄。 |
---|
public function getBasePath()
{
if ($this->_basePath === null) {
$class = new \ReflectionClass($this);
$this->_basePath = dirname($class->getFileName());
}
return $this->_basePath;
}
定義於: 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;
}
定義於: yii\base\Component::getBehaviors()
傳回附加到此元件的所有行為。
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
附加到此組件的行為列表 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
傳回快取元件。
public yii\caching\CacheInterface|null getCache ( ) | ||
return | yii\caching\CacheInterface|null |
快取應用程式元件。如果未啟用元件,則為 Null。 |
---|
public function getCache()
{
return $this->get('cache', false);
}
定義於: 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;
}
定義於: 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;
}
傳回資料庫連線元件。
public yii\db\Connection getDb ( ) | ||
return | yii\db\Connection |
資料庫連線。 |
---|
public function getDb()
{
return $this->get('db');
}
傳回錯誤處理器元件。
public yii\web\ErrorHandler|yii\console\ErrorHandler getErrorHandler ( ) | ||
return | yii\web\ErrorHandler|yii\console\ErrorHandler |
錯誤處理器應用程式組件。 |
---|
public function getErrorHandler()
{
return $this->get('errorHandler');
}
傳回格式器元件。
public yii\i18n\Formatter getFormatter ( ) | ||
return | yii\i18n\Formatter |
格式化器應用程式組件。 |
---|
public function getFormatter()
{
return $this->get('formatter');
}
傳回國際化 (i18n) 元件。
public yii\i18n\I18N getI18n ( ) | ||
return | yii\i18n\I18N |
國際化應用程式組件。 |
---|
public function getI18n()
{
return $this->get('i18n');
}
定義於: yii\base\Module::getInstance()
傳回目前請求的此模組類別的實例。
如果目前未請求模組類別,則將返回 null
。 提供此方法是為了讓您可以從模組內的任何位置存取模組實例。
public static static|null getInstance ( ) | ||
return | static|null |
此模組類別目前請求的實例,如果未請求模組類別,則為 |
---|
public static function getInstance()
{
$class = get_called_class();
return isset(Yii::$app->loadedModules[$class]) ? Yii::$app->loadedModules[$class] : null;
}
定義於: 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;
}
傳回日誌分派器元件。
public yii\log\Dispatcher getLog ( ) | ||
return | yii\log\Dispatcher |
日誌分發器應用程式組件。 |
---|
public function getLog()
{
return $this->get('log');
}
傳回郵件器元件。
public yii\mail\MailerInterface getMailer ( ) | ||
return | yii\mail\MailerInterface |
郵件器應用程式組件。 |
---|---|---|
throws | yii\base\InvalidConfigException |
如果此組件未設定。 |
public function getMailer()
{
return $this->get('mailer');
}
public yii\base\Module|null getModule ( $id, $load = true ) | ||
$id | string |
模組 ID(區分大小寫)。 若要檢索孫子模組,請使用相對於此模組的 ID 路徑(例如 |
$load | boolean |
是否在模組尚未載入時載入模組。 |
return | yii\base\Module|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;
}
定義於: yii\base\Module::getModules()
傳回此模組中的子模組。
public array getModules ( $loadedOnly = false ) | ||
$loadedOnly | boolean |
是否僅返回已載入的子模組。 如果設定為 |
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;
}
傳回請求元件。
public yii\web\Request|yii\console\Request getRequest ( ) | ||
return | yii\web\Request|yii\console\Request |
請求元件。 |
---|
public function getRequest()
{
return $this->get('request');
}
傳回回應元件。
public yii\web\Response|yii\console\Response getResponse ( ) | ||
return | yii\web\Response|yii\console\Response |
回應元件。 |
---|
public function getResponse()
{
return $this->get('response');
}
傳回儲存執行階段檔案的目錄。
public string getRuntimePath ( ) | ||
return | string |
儲存執行階段檔案的目錄。預設為 $basePath 下的 "runtime" 子目錄。 |
---|
public function getRuntimePath()
{
if ($this->_runtimePath === null) {
$this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
}
return $this->_runtimePath;
}
傳回安全性元件。
public yii\base\Security getSecurity ( ) | ||
return | yii\base\Security |
安全性應用程式元件。 |
---|
public function getSecurity()
{
return $this->get('security');
}
傳回此應用程式使用的時區。
這是 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();
}
傳回一個 ID,在目前應用程式中的所有模組中唯一識別此模組。
由於這是應用程式實例,因此它將始終返回空字串。
public string getUniqueId ( ) | ||
return | string |
模組的唯一 ID。 |
---|
public function getUniqueId()
{
return '';
}
傳回此應用程式的 URL 管理器。
public yii\web\UrlManager getUrlManager ( ) | ||
return | yii\web\UrlManager |
此應用程式的 URL 管理器。 |
---|
public function getUrlManager()
{
return $this->get('urlManager');
}
傳回儲存 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;
}
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;
}
傳回視圖物件。
public yii\base\View|yii\web\View getView ( ) | ||
return | yii\base\View|yii\web\View |
用於渲染各種視圖檔案的視圖應用程式元件。 |
---|
public function getView()
{
return $this->get('view');
}
定義於: 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;
}
處理指定的請求。
此方法應返回 yii\base\Response 或其子類別的實例,該實例表示請求的處理結果。
public abstract yii\base\Response handleRequest ( $request ) | ||
$request | yii\base\Request |
要處理的請求 |
return | yii\base\Response |
產生的回應 |
---|
abstract public function handleRequest($request);
傳回一個值,指示定位器是否具有指定的元件定義或已實例化元件。
自版本 2.0.13 起,如果組件未在模組中定義,則將會在父模組中查找。父模組可能是應用程式。
此方法可能會根據 $checkInstance
的值返回不同的結果。
- 如果
$checkInstance
為 false(預設值),則此方法將返回一個值,指示定位器是否具有指定的組件定義。 - 如果
$checkInstance
為 true,此方法將會回傳一個值,指示定位器是否已實例化指定的元件。
public boolean has ( $id, $checkInstance = false ) | ||
$id | string |
組件 ID(例如 |
$checkInstance | boolean |
此方法是否應檢查元件是否為共享且已實例化。 |
return | boolean |
定位器是否具有指定的元件定義,或是否已實例化該元件。 |
---|
public function has($id, $checkInstance = false)
{
return parent::has($id, $checkInstance) || (isset($this->module) && $this->module->has($id, $checkInstance));
}
定義於: 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);
}
定義於: 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;
}
public boolean hasModule ( $id ) | ||
$id | string |
模組 ID。對於孫子模組,請使用相對於此模組的 ID 路徑 (例如 |
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]);
}
定義於: 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);
}
public void init ( ) |
public function init()
{
$this->state = self::STATE_INIT;
$this->bootstrap();
}
定義於: 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;
}
將事件處理常式附加到事件。
事件處理器必須是有效的 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]);
}
}
預先初始化應用程式。
此方法在應用程式建構子的開頭調用。它初始化幾個重要的應用程式屬性。如果您覆寫此方法,請確保您調用父類別的實作。
public void preInit ( &$config ) | ||
$config | array |
應用程式配置 |
throws | yii\base\InvalidConfigException |
---|
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'];
}
}
}
將 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();
}
}
執行應用程式。
這是應用程式的主要入口點。
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;
}
}
定義於: 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) . '".');
}
定義於: 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(例如 |
$definition | mixed |
要在此定位器中註冊的元件定義。它可以是以下之一: |
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));
}
}
public void setAliases ( $aliases ) | ||
$aliases | array |
要定義的路徑別名列表。陣列鍵是別名名稱 (必須以
|
public function setAliases($aliases)
{
foreach ($aliases as $name => $alias) {
Yii::setAlias($name, $alias);
}
}
設定應用程式的根目錄和 @app 別名。
此方法只能在建構子的開頭調用。
public void setBasePath ( $path ) | ||
$path | string |
應用程式的根目錄。 |
throws | yii\base\InvalidArgumentException |
如果目錄不存在。 |
---|
public function setBasePath($path)
{
parent::setBasePath($path);
Yii::setAlias('@app', $this->getBasePath());
}
定義於: 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);
}
}
使用 $config 配置 Yii::$container。
public void setContainer ( $config ) | ||
$config | array |
以名稱-值對形式給定的值 |
public function setContainer($config)
{
Yii::configure(Yii::$container, $config);
}
定義於: yii\base\Module::setControllerPath()
設定包含控制器類別的目錄。
public void setControllerPath ( $path ) | ||
$path | string |
包含控制器類別的根目錄。 |
throws | yii\base\InvalidArgumentException |
如果目錄無效。 |
---|
public function setControllerPath($path)
{
$this->_controllerPath = Yii::getAlias($path);
}
定義於: yii\base\Module::setInstance()
設定目前請求的此模組類別的實例。
public static void setInstance ( $instance ) | ||
$instance | yii\base\Module|null |
此模組類別目前請求的實例。如果為 |
public static function setInstance($instance)
{
if ($instance === null) {
unset(Yii::$app->loadedModules[get_called_class()]);
} else {
Yii::$app->loadedModules[get_class($instance)] = $instance;
}
}
定義於: yii\base\Module::setLayoutPath()
設定包含版面配置檔案的目錄。
public void setLayoutPath ( $path ) | ||
$path | string |
版面配置檔案的根目錄或 路徑別名。 |
throws | yii\base\InvalidArgumentException |
如果目錄無效 |
---|
public function setLayoutPath($path)
{
$this->_layoutPath = Yii::getAlias($path);
}
定義於: yii\base\Module::setModule()
將子模組新增到此模組。
public void setModule ( $id, $module ) | ||
$id | string |
模組 ID。 |
$module | yii\base\Module|array|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;
}
}
}
定義於: 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;
}
}
}
設定儲存執行階段檔案的目錄。
public void setRuntimePath ( $path ) | ||
$path | string |
儲存執行階段檔案的目錄。 |
public function setRuntimePath($path)
{
$this->_runtimePath = Yii::getAlias($path);
Yii::setAlias('@runtime', $this->_runtimePath);
}
設定此應用程式使用的時區。
這是 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);
}
設定儲存 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');
}
定義於: yii\base\Module::setVersion()
設定目前的模組版本。
public void setVersion ( $version ) | ||
$version | string|callable|null |
此模組的版本。版本可以指定為 PHP 回呼,它可以接受模組實例作為參數,並應回傳實際版本。例如:
|
public function setVersion($version)
{
$this->_version = $version;
}
定義於: yii\base\Module::setViewPath()
設定包含視圖檔案的目錄。
public void setViewPath ( $path ) | ||
$path | string |
視圖檔案的根目錄。 |
throws | yii\base\InvalidArgumentException |
如果目錄無效。 |
---|
public function setViewPath($path)
{
$this->_viewPath = Yii::getAlias($path);
}
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);
}
事件詳細資訊
在應用程式成功處理請求後(在回應傳送出去之前)引發的事件。
在應用程式開始處理請求之前引發的事件。
註冊 或 登入 以進行評論。