類別 yii\base\Controller
Controller 是包含控制器邏輯之類別的基底類別。
關於 Controller 的更多詳細資訊和使用方式,請參閱控制器指南文章。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$action | yii\base\Action|null | 目前正在執行的動作。 | yii\base\Controller |
$behaviors | yii\base\Behavior[] | 附加到此組件的行為列表。 | yii\base\Component |
$defaultAction | string | 當請求中未指定動作 ID 時,使用的動作 ID。 | yii\base\Controller |
$id | string | 此控制器的 ID。 | yii\base\Controller |
$layout | string|null|false | 應用於此控制器視圖的版面配置名稱。 | yii\base\Controller |
$module | yii\base\Module | 此控制器所屬的模組。 | yii\base\Controller |
$modules | yii\base\Module[] | 此控制器所位於的所有祖先模組。 | yii\base\Controller |
$request | yii\base\Request|array|string | 請求。 | yii\base\Controller |
$response | yii\base\Response|array|string | 回應。 | yii\base\Controller |
$route | string | 當前請求的路由(模組 ID、控制器 ID 和動作 ID)。 | yii\base\Controller |
$uniqueId | string | 以模組 ID 為前綴的控制器 ID(如果有的話)。 | yii\base\Controller |
$view | yii\base\View|yii\web\View | 可用於渲染視圖或視圖檔案的視圖物件。 | yii\base\Controller |
$viewPath | string | 包含此控制器視圖檔案的目錄。 | yii\base\Controller |
公開方法
事件
事件 | 類型 | 描述 | 定義於 |
---|---|---|---|
EVENT_AFTER_ACTION | yii\base\ActionEvent | 在執行控制器動作後立即引發的事件。 | yii\base\Controller |
EVENT_BEFORE_ACTION | yii\base\ActionEvent | 在執行控制器動作前立即引發的事件。 | yii\base\Controller |
屬性詳細資訊
目前正在執行的動作。當 yii\base\Application 呼叫 run() 以執行動作時,將會設定此屬性。
方法詳細資訊
定義於: 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 ( $id, $module, $config = [] ) | ||
$id | string |
此控制器的 ID。 |
$module | yii\base\Module |
此控制器所屬的模組。 |
$config | array |
將用於初始化物件屬性的名稱-值配對。 |
public function __construct($id, $module, $config = [])
{
$this->id = $id;
$this->module = $module;
parent::__construct($config);
}
定義於: yii\base\Component::__get()
傳回組件屬性的值。
此方法將依以下順序檢查並採取相應措施
- 由 getter 定義的屬性:傳回 getter 結果
- 行為的屬性:傳回行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $component->property;
時,它會被隱式呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值或行為屬性的值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫。 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__isset()
檢查屬性是否已設定,即已定義且非 null。
此方法將依以下順序檢查並採取相應措施
- 由 setter 定義的屬性:傳回屬性是否已設定
- 行為的屬性:傳回屬性是否已設定
- 對於不存在的屬性,傳回
false
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property)
時,它會被隱式呼叫。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
指定的屬性是否已設定 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
定義於: 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);
}
宣告控制器的外部動作。
此方法旨在被覆寫,以宣告控制器的外部動作。它應傳回一個陣列,其中陣列鍵是動作 ID,而陣列值是對應的動作類別名稱或動作組態陣列。例如:
return [
'action1' => 'app\components\Action1',
'action2' => [
'class' => 'app\components\Action2',
'property1' => 'value1',
'property2' => 'value2',
],
];
Yii::createObject() 將稍後用於使用此處提供的組態來建立請求的動作。
public array actions ( ) |
public function actions()
{
return [];
}
此方法會在動作執行後立即調用。
此方法將觸發 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);
}
}
此方法會在動作執行前立即調用。
此方法將觸發 EVENT_BEFORE_ACTION 事件。此方法的傳回值將決定是否應繼續執行動作。
如果動作不應執行,則應在 beforeAction
程式碼內處理請求,方法是提供必要的輸出或重新導向請求。否則,回應將為空。
如果您覆寫此方法,您的程式碼應如下所示:
public function beforeAction($action)
{
// your custom code here, if you want the code to run before action filters,
// which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl
if (!parent::beforeAction($action)) {
return false;
}
// other 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 [];
}
將參數綁定到動作。
此方法由 yii\base\Action 在開始使用給定參數執行時調用。
public array bindActionParams ( $action, $params ) | ||
$action | yii\base\Action |
要與參數綁定的動作 (action)。 |
$params | array |
要綁定到動作 (action) 的參數。 |
return | array |
動作 (action) 可以運行的有效參數。 |
---|
public function bindActionParams($action, $params)
{
return [];
}
根據動作方法簽名中的類型和名稱填寫參數。
protected void bindInjectedParams ( ReflectionType $type, $name, &$args, &$requestedParams ) | ||
$type | ReflectionType |
動作 (action) 參數的反射型別。 |
$name | string |
參數的名稱。 |
$args | array |
動作 (action) 的參數陣列,此函數可能會在其中附加項目。 |
$requestedParams | array |
包含請求參數的陣列,此函數可能會在其中寫入特定鍵。 |
throws | yii\base\ErrorException |
當我們無法載入所需的服務時。 |
---|---|---|
throws | yii\base\InvalidConfigException |
當 DI 配置中存在錯誤時拋出。 |
throws | yii\di\NotInstantiableException |
當定義無法解析為具體類別時(例如介面型別提示),且容器中沒有適當的定義時拋出。 |
final protected function bindInjectedParams(\ReflectionType $type, $name, &$args, &$requestedParams)
{
// Since it is not a builtin type it must be DI injection.
$typeName = $type->getName();
if (($component = $this->module->get($name, false)) instanceof $typeName) {
$args[] = $component;
$requestedParams[$name] = 'Component: ' . get_class($component) . " \$$name";
} elseif ($this->module->has($typeName) && ($service = $this->module->get($typeName)) instanceof $typeName) {
$args[] = $service;
$requestedParams[$name] = 'Module ' . get_class($this->module) . " DI: $typeName \$$name";
} elseif (\Yii::$container->has($typeName) && ($service = \Yii::$container->get($typeName)) instanceof $typeName) {
$args[] = $service;
$requestedParams[$name] = "Container DI: $typeName \$$name";
} elseif ($type->allowsNull()) {
$args[] = null;
$requestedParams[$name] = "Unavailable service: $name";
} else {
throw new Exception('Could not load required service: ' . $name);
}
}
定義於: yii\base\Component::canGetProperty()
傳回一個值,指示是否可以讀取屬性。
如果符合以下條件,則可以讀取屬性:
- 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為具有給定名稱的可讀屬性(當
$checkBehaviors
為 true 時)。
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為 (behavior) 的屬性視為此組件 (component) 的屬性 |
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 |
是否將行為 (behavior) 的屬性視為此組件 (component) 的屬性 |
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();
}
根據給定的動作 ID 建立動作。
此方法首先檢查動作 (action) ID 是否已在 actions() 中宣告。如果是,它將使用那裡宣告的配置來建立動作 (action) 物件。如果不是,它將尋找控制器方法,其名稱格式為 actionXyz
,其中 xyz
是動作 (action) ID。如果找到,將建立並傳回代表該方法的 yii\base\InlineAction。
public yii\base\Action|null createAction ( $id ) | ||
$id | string |
動作 (action) ID。 |
return | yii\base\Action|null |
新建立的動作 (action) 實例。如果 ID 無法解析為任何動作 (action),則為 Null。 |
---|
public function createAction($id)
{
if ($id === '') {
$id = $this->defaultAction;
}
$actionMap = $this->actions();
if (isset($actionMap[$id])) {
return Yii::createObject($actionMap[$id], [$id, $this]);
}
if (preg_match('/^(?:[a-z0-9_]+-)*[a-z0-9_]+$/', $id)) {
$methodName = 'action' . str_replace(' ', '', ucwords(str_replace('-', ' ', $id)));
if (method_exists($this, $methodName)) {
$method = new \ReflectionMethod($this, $methodName);
if ($method->isPublic() && $method->getName() === $methodName) {
return new InlineAction($id, $this, $methodName);
}
}
}
return null;
}
定義於: 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)。如果行為 (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;
}
定義於: yii\base\Component::detachBehaviors()
從組件中分離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義於: 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 string|boolean findLayoutFile ( $view ) | ||
$view | yii\base\View |
要渲染佈局檔案的視圖 (view) 物件。 |
return | string|boolean |
佈局檔案路徑,或如果不需要佈局則為 false。請參考 render() 關於如何指定此參數。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果使用無效的路徑別名來指定佈局。 |
public function findLayoutFile($view)
{
$module = $this->module;
$layout = null;
if (is_string($this->layout)) {
$layout = $this->layout;
} elseif ($this->layout === null) {
while ($module !== null && $module->layout === null) {
$module = $module->module;
}
if ($module !== null && is_string($module->layout)) {
$layout = $module->layout;
}
}
if ($layout === null) {
return false;
}
if (strncmp($layout, '@', 1) === 0) {
$file = Yii::getAlias($layout);
} elseif (strncmp($layout, '/', 1) === 0) {
$file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . substr($layout, 1);
} else {
$file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $layout;
}
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
return $file;
}
$path = $file . '.' . $view->defaultExtension;
if ($view->defaultExtension !== 'php' && !is_file($path)) {
$path = $file . '.php';
}
return $path;
}
定義於: yii\base\Component::getBehavior()
傳回指定的行為物件。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
行為 (behavior) 名稱 |
return | yii\base\Behavior|null |
行為 (behavior) 物件,如果行為 (behavior) 不存在則為 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[] |
附加到此組件 (component) 的行為 (behavior) 清單 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
傳回此控制器的所有祖先模組。
陣列中的第一個模組是最外層的模組(即應用程式實例),而最後一個是最內層的模組。
public yii\base\Module[] getModules ( ) | ||
return | yii\base\Module[] |
此控制器所位於的所有祖先模組。 |
---|
public function getModules()
{
$modules = [$this->module];
$module = $this->module;
while ($module->module !== null) {
array_unshift($modules, $module->module);
$module = $module->module;
}
return $modules;
}
傳回當前請求的路由。
public string getRoute ( ) | ||
return | string |
當前請求的路由(模組 ID、控制器 ID 和動作 ID)。 |
---|
public function getRoute()
{
return $this->action !== null ? $this->action->getUniqueId() : $this->getUniqueId();
}
傳回控制器的唯一 ID。
public string getUniqueId ( ) | ||
return | string |
以模組 ID 為前綴的控制器 ID(如果有的話)。 |
---|
public function getUniqueId()
{
return $this->module instanceof Application ? $this->id : $this->module->getUniqueId() . '/' . $this->id;
}
傳回可用於渲染視圖或視圖檔案的視圖物件。
render()、renderPartial() 和 renderFile() 方法將使用此視圖 (view) 物件來實作實際的視圖 (view) 渲染。如果未設定,它將預設為 "view" 應用程式組件 (component)。
public yii\base\View|yii\web\View getView ( ) | ||
return | yii\base\View|yii\web\View |
可用於渲染視圖或視圖檔案的視圖物件。 |
---|
public function getView()
{
if ($this->_view === null) {
$this->_view = Yii::$app->getView();
}
return $this->_view;
}
public string getViewPath ( ) | ||
return | string |
包含此控制器視圖檔案的目錄。 |
---|
public function getViewPath()
{
if ($this->_viewPath === null) {
$this->_viewPath = $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id;
}
return $this->_viewPath;
}
定義於: 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 |
是否將行為 (behavior) 的方法視為此組件 (component) 的方法 |
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;
}
定義於: yii\base\Component::hasProperty()
傳回一個值,指示是否為此組件定義了屬性。
如果符合以下條件,則定義了屬性:
- 類別具有與指定名稱關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數(當
$checkVars
為 true 時); - 附加的行為 (behavior) 具有給定名稱的屬性(當
$checkBehaviors
為 true 時)。
另請參閱
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為 (behavior) 的屬性視為此組件 (component) 的屬性 |
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()
{
parent::init();
$this->request = Instance::ensure($this->request, Request::className());
$this->response = Instance::ensure($this->response, Response::className());
}
定義於: 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 回呼 (callback)。以下是一些範例
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
事件處理常式必須使用以下簽章 (signature) 定義:
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]);
}
}
渲染視圖並在可用時套用版面配置。
要渲染的視圖 (view) 可以使用以下格式之一指定
- 路徑別名(例如 "@app/views/site/index");
- 應用程式內的絕對路徑(例如 "//site/index"):視圖 (view) 名稱以雙斜線開頭。實際的視圖 (view) 檔案將在應用程式的 視圖路徑 下尋找。
- 模組內的絕對路徑(例如 "/site/index"):視圖 (view) 名稱以單斜線開頭。實際的視圖 (view) 檔案將在 $module 的 視圖路徑 下尋找。
- 相對路徑(例如 "index"):實際的視圖 (view) 檔案將在 $viewPath 下尋找。
為了確定應套用哪個佈局 (layout),將執行以下兩個步驟
- 在第一步中,它確定佈局 (layout) 名稱和上下文模組
- 如果 $layout 指定為字串,則將其用作佈局 (layout) 名稱,並將 $module 用作上下文模組;
- 如果 $layout 為 null,則搜尋此控制器的所有父模組,並找到第一個 layout 不為 null 的模組。佈局 (layout) 和相應的模組分別用作佈局 (layout) 名稱和上下文模組。如果找不到這樣的模組,或者相應的佈局 (layout) 不是字串,它將傳回 false,表示沒有適用的佈局 (layout)。
- 在第二步中,它根據先前找到的佈局 (layout) 名稱和上下文模組確定實際的佈局 (layout) 檔案。佈局 (layout) 名稱可以是
- 路徑別名(例如 "@app/views/layouts/main");
- 絕對路徑(例如 "/main"):佈局 (layout) 名稱以斜線開頭。實際的佈局 (layout) 檔案將在應用程式的 佈局路徑 下尋找;
- 相對路徑(例如 "main"):實際的佈局 (layout) 檔案將在上下文模組的 佈局路徑 下尋找。
如果佈局 (layout) 名稱不包含檔案副檔名,它將使用預設的 .php
。
public string render ( $view, $params = [] ) | ||
$view | string |
視圖 (view) 名稱。 |
$params | array |
應在視圖 (view) 中提供的參數(名稱-值對)。這些參數在佈局 (layout) 中不可用。 |
return | string |
渲染結果。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果視圖 (view) 檔案或佈局 (layout) 檔案不存在。 |
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
透過套用版面配置來渲染靜態字串。
public string renderContent ( $content ) | ||
$content | string |
正在渲染的靜態字串 |
return | string |
帶有給定靜態字串作為 |
---|
public function renderContent($content)
{
$layoutFile = $this->findLayoutFile($this->getView());
if ($layoutFile !== false) {
return $this->getView()->renderFile($layoutFile, ['content' => $content], $this);
}
return $content;
}
渲染視圖檔案。
public string renderFile ( $file, $params = [] ) | ||
$file | string |
要渲染的視圖 (view) 檔案。這可以是檔案路徑或 路徑別名。 |
$params | array |
應在視圖 (view) 中提供的參數(名稱-值對)。 |
return | string |
渲染結果。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果視圖 (view) 檔案不存在。 |
public function renderFile($file, $params = [])
{
return $this->getView()->renderFile($file, $params, $this);
}
渲染視圖,但不套用版面配置。
此方法與 render() 的不同之處在於,它不會套用任何版面配置。
public string renderPartial ( $view, $params = [] ) | ||
$view | string |
視圖名稱。請參考 render() 以了解如何指定視圖名稱。 |
$params | array |
應在視圖 (view) 中提供的參數(名稱-值對)。 |
return | string |
渲染結果。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果視圖 (view) 檔案不存在。 |
public function renderPartial($view, $params = [])
{
return $this->getView()->render($view, $params, $this);
}
執行以路由形式指定的請求。
路由可以是此控制器內動作的 ID,或是包含模組 ID、控制器 ID 和動作 ID 的完整路由。如果路由以斜線 '/' 開頭,則路由的解析將從應用程式開始;否則,它將從此控制器的父模組開始。
另請參閱 runAction()。
public mixed run ( $route, $params = [] ) | ||
$route | string |
要處理的路由,例如 'view'、'comment/view'、'/admin/comment/view'。 |
$params | array |
要傳遞給動作的參數。 |
return | mixed |
動作的結果。 |
---|
public function run($route, $params = [])
{
$pos = strpos($route, '/');
if ($pos === false) {
return $this->runAction($route, $params);
} elseif ($pos > 0) {
return $this->module->runAction($route, $params);
}
return Yii::$app->runAction(ltrim($route, '/'), $params);
}
public mixed runAction ( $id, $params = [] ) | ||
$id | string |
要執行的動作的 ID。 |
$params | array |
要傳遞給動作的參數(名稱-值 對)。 |
return | mixed |
動作的結果。 |
---|---|---|
throws | yii\base\InvalidRouteException |
如果請求的動作 ID 無法成功解析為動作。 |
public function runAction($id, $params = [])
{
$action = $this->createAction($id);
if ($action === null) {
throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
}
Yii::debug('Route to run: ' . $action->getUniqueId(), __METHOD__);
if (Yii::$app->requestedAction === null) {
Yii::$app->requestedAction = $action;
}
$oldAction = $this->action;
$this->action = $action;
$modules = [];
$runAction = true;
// call beforeAction on modules
foreach ($this->getModules() as $module) {
if ($module->beforeAction($action)) {
array_unshift($modules, $module);
} else {
$runAction = false;
break;
}
}
$result = null;
if ($runAction && $this->beforeAction($action)) {
// run the action
$result = $action->runWithParams($params);
$result = $this->afterAction($action, $result);
// call afterAction on modules
foreach ($modules as $module) {
/* @var $module Module */
$result = $module->afterAction($action, $result);
}
}
if ($oldAction !== null) {
$this->action = $oldAction;
}
return $result;
}
設定此控制器要使用的視圖物件。
public void setView ( $view ) | ||
$view | yii\base\View|yii\web\View |
可用於渲染視圖或視圖檔案的視圖物件。 |
public function setView($view)
{
$this->_view = $view;
}
設定包含視圖檔案的目錄。
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);
}
事件詳細資訊
在執行控制器動作後立即引發的事件。
在執行控制器動作之前立即觸發的事件。您可以將 yii\base\ActionEvent::$isValid 設定為 false 以取消動作執行。
為了發表評論,請註冊或登入。