類別 yii\base\View
公開屬性
屬性 | 類型 | 說明 | 由 |
---|---|---|---|
$behaviors | yii\base\Behavior[] | 附加到此組件的行為列表。 | yii\base\Component |
$blocks | array | 具名輸出區塊的列表。 | yii\base\View |
$context | yii\base\ViewContextInterface | renderFile() 方法在其下被調用的上下文。 | yii\base\View |
$defaultExtension | string | 預設視圖檔案副檔名。 | yii\base\View |
$dynamicContents | yii\base\DynamicContentAwareInterface[] | 支援動態內容的類別實例。 | yii\base\View |
$dynamicPlaceholders | array | 佔位符號的列表。 | yii\base\View |
$params | array | 在視圖範本之間共享的自訂參數。 | yii\base\View |
$renderers | array|null | 可用渲染器的列表,以其對應的支援檔案副檔名作為索引。 | yii\base\View |
$theme | yii\base\Theme|array|string|null | 主題物件或用於建立主題物件的配置。 | yii\base\View |
$viewFile | string|boolean | 當前正在渲染的視圖檔案。 | yii\base\View |
公開方法
受保護的方法
方法 | 說明 | 由 |
---|---|---|
findViewFile() | 根據給定的視圖名稱尋找視圖檔案。 | yii\base\View |
getRequestedViewFile() | yii\base\View |
事件
事件 | 類型 | 說明 | 由 |
---|---|---|---|
EVENT_AFTER_RENDER | yii\base\ViewEvent | 此事件在 renderFile() 呈現視圖檔案之後立即觸發。 | yii\base\View |
EVENT_BEFORE_RENDER | yii\base\ViewEvent | 此事件在 renderFile() 呈現視圖檔案之前立即觸發。 | yii\base\View |
EVENT_BEGIN_PAGE | yii\base\Event | 此事件由 beginPage() 觸發。 | yii\base\View |
EVENT_END_PAGE | yii\base\Event | 此事件由 endPage() 觸發。 | yii\base\View |
屬性詳細資訊
具名輸出區塊的清單。鍵名是區塊名稱,值是相應的區塊內容。您可以呼叫 beginBlock() 和 endBlock() 來捕獲視圖的小片段。它們稍後可以通過此屬性在其他地方訪問。
renderFile() 方法在其下被調用的上下文。
預設視圖檔案副檔名。如果視圖檔案名稱沒有檔案副檔名,則會將其附加到視圖檔案名稱。
支援動態內容的類別實例。
佔位符號的列表。
可用渲染器的清單,依其對應支援的檔案副檔名索引。每個渲染器可以是視圖渲染器物件,或是用於建立渲染器物件的配置。例如,以下配置同時啟用 Smarty 和 Twig 視圖渲染器
[
'tpl' => ['class' => 'yii\smarty\ViewRenderer'],
'twig' => ['class' => 'yii\twig\ViewRenderer'],
]
如果給定的視圖檔案沒有可用的渲染器,則視圖檔案將被視為普通的 PHP 並通過 renderPhpFile() 呈現。
主題物件或用於建立主題物件的配置。如果未設定,則表示未啟用主題。
方法詳細資訊
定義於: yii\base\Component::__call()
呼叫指定的非類別方法。
此方法將檢查任何附加的行為是否具有指定的名稱方法,如果有的話,將執行它。
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當調用未知方法時,它將被隱式呼叫。
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
$params |
傳遞給方法的參數 | return |
mixed |
---|---|---|
方法傳回值 | throws |
yii\base\UnknownMethodException |
public function __call($name, $params)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
它會移除所有行為,因為它們都附加到舊物件上。 |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
建構子。
定義於: yii\base\BaseObject::__construct()
- 預設實作會執行兩件事
- 使用給定的配置
$config
初始化物件。
呼叫 init()。
- 如果子類別中覆寫了此方法,建議
- 建構子的最後一個參數是配置陣列,例如此處的
$config
。
在建構子的結尾呼叫父類別實作。 | ||
public void __construct ( $config = [] ) | array |
$config |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
返回組件屬性的值。
定義於: yii\base\Component::__get()
- 此方法將按以下順序檢查並相應地執行操作
- 由 getter 定義的屬性:傳回 getter 結果
行為的屬性:傳回行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 $value = $component->property;
時,它將被隱式呼叫。
另請參閱 __set()。 | ||
$name | string |
public mixed __get ( $name ) |
傳遞給方法的參數 | return |
$name |
---|---|---|
方法傳回值 | 屬性名稱 |
return |
方法傳回值 | 屬性值或行為的屬性值 |
throws |
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);
}
如果未定義屬性
檢查屬性是否已設定,即已定義且非 null。
定義於: yii\base\Component::__get()
定義於: yii\base\Component::__isset()
由 setter 定義的屬性:傳回屬性是否已設定
行為的屬性:傳回屬性是否已設定 | ||
$name | string |
對於不存在的屬性,傳回 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
---|
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;
}
由 setter 定義的屬性:設定屬性值 | ||
$name | string |
對於不存在的屬性,傳回 |
格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz" | return |
格式為 "as xyz" 的行為:附加名為 "xyz" 的行為 |
方法傳回值 | 屬性名稱 |
return |
---|---|---|
方法傳回值 | 屬性值或行為的屬性值 |
行為的屬性:設定行為屬性值 |
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);
}
$component->property = $value;
時,它將被隱式呼叫。另請參閱 __get()。
將組件屬性設定為 null。
定義於: yii\base\Component::__get()
- public void __set ( $name, $value )
- $name
屬性名稱
$value
屬性值 | ||
$name | string |
public mixed __get ( $name ) |
方法傳回值 | 屬性值或行為的屬性值 |
throws |
---|
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);
}
為動態內容新增佔位符號。
如果屬性是唯讀的。
__unset() public method | ||
定義於: yii\base\Component::__unset() | ||
由 setter 定義的屬性:將屬性值設定為 null | string |
行為的屬性:將屬性值設定為 null |
public function addDynamicPlaceholder($placeholder, $statements)
{
foreach ($this->cacheStack as $cache) {
if ($cache instanceof DynamicContentAwareInterface) {
$cache->addDynamicPlaceholder($placeholder, $statements);
} else {
// TODO: Remove in 2.1
$cache->dynamicPlaceholders[$placeholder] = $statements;
}
}
$this->dynamicPlaceholders[$placeholder] = $statements;
}
unset($component->property)
時,它將被隱式呼叫。在 renderFile() 渲染視圖檔案後立即調用此方法。
public void __unset ( $name ) | ||
$viewFile | string |
throws |
$params | array |
yii\base\UnknownPropertyException |
如果屬性是唯讀的。 | string |
public function afterRender($viewFile, $params, &$output)
{
if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
$event = new ViewEvent([
'viewFile' => $viewFile,
'params' => $params,
]);
$event->output =& $output;
$this->trigger(self::EVENT_AFTER_RENDER, $event);
}
}
用於產生動態內容的 PHP 陳述式。 | ||
$name | string | |
預設實作將觸發 EVENT_AFTER_RENDER 事件。如果您覆寫此方法,請確保先呼叫父類別實作。 | public void afterRender ( $viewFile, $params, &$output ) |
$viewFile
|
傳遞給方法的參數 | yii\base\Behavior |
$output |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
attachBehavior() public method
將行為列表附加到組件。
定義於: yii\base\Component::attachBehavior()
此方法將根據給定的配置建立行為物件。之後,將通過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此組件。
另請參閱 detachBehavior()。 | ||
$behaviors | array |
public yii\base\Behavior attachBehavior ( $name, $behavior ) |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
在 renderFile() 渲染視圖檔案之前立即調用此方法。
行為的名稱。
$behavior | ||
$viewFile | string | |
$params | array |
yii\base\UnknownPropertyException |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
行為配置。可以是以下之一 |
---|
public function beforeRender($viewFile, $params)
{
$event = new ViewEvent([
'viewFile' => $viewFile,
'params' => $params,
]);
$this->trigger(self::EVENT_BEFORE_RENDER, $event);
return $event->isValid;
}
開始記錄區塊。
指定行為類別的字串
將傳遞給 Yii::createObject() 以建立行為物件的物件配置陣列。 | ||
return | string |
行為物件 |
attachBehaviors() public method | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
|
傳遞給方法的參數 | 每個行為都由其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或是用於建立行為的配置陣列。 |
另請參閱 attachBehavior()。 |
---|
public function beginBlock($id, $renderInPlace = false)
{
return Block::begin([
'id' => $id,
'renderInPlace' => $renderInPlace,
'view' => $this,
]);
}
開始片段快取。
$behaviors
if ($this->beginCache($id)) {
// ...generate content here
$this->endCache();
}
要附加到組件的行為清單 | ||
return | string | |
預設實作將觸發 EVENT_BEFORE_RENDER 事件。如果您覆寫此方法,請確保先呼叫父類別實作。 | array |
public boolean beforeRender ( $viewFile, $params ) |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
$viewFile |
---|
public function beginCache($id, $properties = [])
{
$properties['id'] = $id;
$properties['view'] = $this;
/* @var $cache FragmentCache */
$cache = FragmentCache::begin($properties);
if ($cache->getCachedContent() !== false) {
$this->endCache();
return false;
}
return true;
}
開始渲染要由指定視圖裝飾的內容。
return
<?php $this->beginContent('@app/views/layouts/base.php'); ?>
//...layout content here...
<?php $this->endContent(); ?>
boolean
是否繼續呈現視圖檔案。 | ||
$viewFile | string | |
$params | array |
此方法是開始 yii\widgets\Block 的快捷方式。 |
傳遞給方法的參數 | public yii\widgets\Block beginBlock ( $id, $renderInPlace = false ) |
$id |
---|
public function beginContent($viewFile, $params = [])
{
return ContentDecorator::begin([
'viewFile' => $viewFile,
'params' => $params,
'view' => $this,
]);
}
標記頁面的開始。
$renderInPlace |
public function beginPage()
{
ob_start();
ob_implicit_flush(false);
$this->trigger(self::EVENT_BEGIN_PAGE);
}
return
返回此組件應表現為的行為列表。
yii\widgets\Block
Block Widget 實例
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
在這個方法中宣告的行為將會自動附加到元件上 (依照需求)。
public array behaviors ( ) | ||
傳遞給方法的參數 | array |
行為設定。 |
---|
public function behaviors()
{
return [];
}
Defined in: yii\base\Component::canGetProperty()
返回一個值,指示屬性是否可以讀取。
在以下情況下,屬性可以被讀取:
- 類別具有與指定名稱相關聯的 getter 方法 (在此情況下,屬性名稱不區分大小寫);
- 類別具有與指定名稱相同的成員變數 (當
$checkVars
為 true 時); - 已附加的行為具有給定名稱的可讀屬性 (當
$checkBehaviors
為 true 時)。
參見 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
public mixed __get ( $name ) |
$checkVars | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將成員變數視為屬性 |
$checkBehaviors | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將行為的屬性視為此元件的屬性 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
屬性是否可以被讀取 |
---|
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;
}
Defined in: yii\base\Component::canSetProperty()
返回一個值,指示屬性是否可以設定。
在以下情況下,屬性可以被寫入:
- 類別具有與指定名稱相關聯的 setter 方法 (在此情況下,屬性名稱不區分大小寫);
- 類別具有與指定名稱相同的成員變數 (當
$checkVars
為 true 時); - 已附加的行為具有給定名稱的可寫屬性 (當
$checkBehaviors
為 true 時)。
參見 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
public mixed __get ( $name ) |
$checkVars | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將成員變數視為屬性 |
$checkBehaviors | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將行為的屬性視為此元件的屬性 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
屬性是否可以被寫入 |
---|
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
。
Defined in: yii\base\BaseObject::className()
返回此類別的完整限定名稱。
public static string className ( ) | ||
傳遞給方法的參數 | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
行為的名稱。 |
傳遞給方法的參數 | 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;
}
Defined in: yii\base\Component::detachBehaviors()
從組件分離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
標記頁面的結束。
public void endPage ( ) |
public function endPage()
{
$this->trigger(self::EVENT_END_PAGE);
ob_end_flush();
}
Defined in: 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);
}
}
}
評估給定的 PHP 語句。
此方法主要在內部用於實作動態內容功能。
public mixed evaluateDynamicContent ( $statements ) | ||
由 setter 定義的屬性:將屬性值設定為 null | string |
要被評估的 PHP 語句。 |
傳遞給方法的參數 | return |
PHP 語句的回傳值。 |
---|
public function evaluateDynamicContent($statements)
{
return eval($statements);
}
根據給定的視圖名稱尋找視圖檔案。
protected string findViewFile ( $view, $context = null ) | ||
$view | string |
視圖名稱或視圖檔案的路徑別名。關於如何指定此參數,請參考 render()。 |
$context | object|null |
要指派給視圖的上下文,稍後可以在視圖中透過 $context 存取。如果上下文實作了 yii\base\ViewContextInterface,它也可以用於定位與相對視圖名稱相對應的視圖檔案。 |
傳遞給方法的參數 | string |
視圖檔案路徑。請注意,該檔案可能不存在。 |
---|---|---|
方法傳回值 | 屬性值或行為的屬性值 |
如果給定了相對視圖名稱,但沒有活動的上下文來確定對應的視圖檔案。 |
protected function findViewFile($view, $context = null)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/main"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '//', 2) === 0) {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '/', 1) === 0) {
// e.g. "/site/index"
if (Yii::$app->controller !== null) {
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} else {
throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
}
} elseif ($context instanceof ViewContextInterface) {
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== false) {
$file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
} else {
throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
}
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
return $file;
}
$path = $file . '.' . $this->defaultExtension;
if ($this->defaultExtension !== 'php' && !is_file($path)) {
$path = $file . '.php';
}
return $path;
}
Defined in: yii\base\Component::getBehavior()
返回指定的行為物件。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
行為名稱 |
傳遞給方法的參數 | yii\base\Behavior|null |
行為物件,如果行為不存在則為 null |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
Defined in: yii\base\Component::getBehaviors()
返回附加到此組件的所有行為。
public yii\base\Behavior[] getBehaviors ( ) | ||
傳遞給方法的參數 | yii\base\Behavior[] |
附加到此元件的行為列表 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
返回當前活動的動態內容類別實例列表。
public yii\base\DynamicContentAwareInterface[] getDynamicContents ( ) | ||
傳遞給方法的參數 | yii\base\DynamicContentAwareInterface[] |
支援動態內容的類別實例。 |
---|
public function getDynamicContents()
{
return $this->cacheStack;
}
返回動態內容的佔位符號列表。此方法在內部用於實作內容快取功能。
public array getDynamicPlaceholders ( ) | ||
傳遞給方法的參數 | array |
佔位符號的列表。 |
---|
public function getDynamicPlaceholders()
{
return $this->dynamicPlaceholders;
}
protected string|boolean getRequestedViewFile ( ) | ||
傳遞給方法的參數 | string|boolean |
目前正在呈現的請求視圖。如果沒有視圖檔案正在呈現,則為 False。 |
---|
protected function getRequestedViewFile()
{
return empty($this->_viewFiles) ? false : end($this->_viewFiles)['requested'];
}
public string|boolean getViewFile ( ) | ||
傳遞給方法的參數 | string|boolean |
目前正在呈現的視圖檔案。如果沒有視圖檔案正在呈現,則為 False。 |
---|
public function getViewFile()
{
return empty($this->_viewFiles) ? false : end($this->_viewFiles)['resolved'];
}
Defined in: yii\base\Component::hasEventHandlers()
返回一個值,指示是否有任何處理程序附加到指定的事件。
public boolean hasEventHandlers ( $name ) | ||
$name | string |
事件名稱 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否有任何處理程序附加到該事件。 |
---|
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);
}
Defined in: yii\base\Component::hasMethod()
返回一個值,指示是否已定義方法。
在以下情況下,方法被定義:
- 類別具有指定名稱的方法
- 已附加的行為具有給定名稱的方法 (當
$checkBehaviors
為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
public mixed __get ( $name ) |
$checkBehaviors | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將行為的方法視為此元件的方法 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
方法是否被定義 |
---|
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;
}
Defined in: yii\base\Component::hasProperty()
返回一個值,指示是否為此組件定義了屬性。
在以下情況下,屬性被定義:
- 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在此情況下,屬性名稱不區分大小寫);
- 類別具有與指定名稱相同的成員變數 (當
$checkVars
為 true 時); - 已附加的行為具有給定名稱的屬性 (當
$checkBehaviors
為 true 時)。
參見
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
public mixed __get ( $name ) |
$checkVars | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將成員變數視為屬性 |
$checkBehaviors | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將行為的屬性視為此元件的屬性 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
屬性是否被定義 |
---|
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();
if (is_array($this->theme)) {
if (!isset($this->theme['class'])) {
$this->theme['class'] = 'yii\base\Theme';
}
$this->theme = Yii::createObject($this->theme);
} elseif (is_string($this->theme)) {
$this->theme = Yii::createObject($this->theme);
}
}
Defined in: yii\base\Component::off()
從此組件分離現有的事件處理程序。
此方法與 on() 相反。
注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理程序,而使用與此萬用字元匹配的純文字名稱註冊的處理程序將會保留。
參見 on()。
public boolean off ( $name, $handler = null ) | ||
$name | string |
事件名稱 |
$handler | callable|null |
要移除的事件處理程序。如果為 null,則會移除附加到指定事件的所有處理程序。 |
傳遞給方法的參數 | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
如果找到並分離處理程序 |
---|
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;
}
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 | return |
當事件被觸發時,要傳遞給事件處理程序的資料。當調用事件處理程序時,可以透過 yii\base\Event::$data 存取此資料。 |
$append | 請勿直接呼叫此方法,因為它是 PHP 魔術方法,當執行 isset($component->property) 時,它將被隱式呼叫。 |
是否將新的事件處理程序附加到現有處理程序列表的末尾。如果為 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 popDynamicContent ( ) |
public function popDynamicContent()
{
array_pop($this->cacheStack);
}
將支援動態內容的類別實例新增到當前活動的動態內容類別實例列表的末尾。
public yii\base\DynamicContentAwareInterface $instance ) | ||
$instance | yii\base\DynamicContentAwareInterface |
支援動態內容的類別實例。 |
public function pushDynamicContent(DynamicContentAwareInterface $instance)
{
$this->cacheStack[] = $instance;
}
渲染視圖。
要呈現的視圖可以使用以下格式之一指定:
- 路徑別名 (例如 "@app/views/site/index");
- 應用程式內的絕對路徑 (例如 "//site/index"):視圖名稱以雙斜線開頭。實際的視圖檔案將會在應用程式的視圖路徑下尋找。
- 目前模組內的絕對路徑 (例如 "/site/index"):視圖名稱以單斜線開頭。實際的視圖檔案將會在目前模組的視圖路徑下尋找。
- 相對視圖 (例如 "index"):視圖名稱不以
@
或/
開頭。對應的視圖檔案將會在視圖$context
的 視圖路徑下尋找。如果未提供$context
,則將會在包含目前正在呈現的視圖的目錄下尋找 (也就是說,當在另一個視圖中呈現視圖時會發生這種情況)。
參見 renderFile()。
public string render ( $view, $params = [], $context = null ) | ||
$view | string |
視圖名稱。 |
$params | array |
將被提取並在視圖檔案中提供的參數 (名稱-值 對)。 |
$context | object|null |
要指派給視圖的上下文,稍後可以在視圖中透過 $context 存取。如果上下文實作了 yii\base\ViewContextInterface,它也可以用於定位與相對視圖名稱相對應的視圖檔案。 |
傳遞給方法的參數 | string |
呈現結果 |
---|---|---|
方法傳回值 | yii\base\ViewNotFoundException |
如果視圖檔案不存在。 |
方法傳回值 | 屬性值或行為的屬性值 |
如果視圖無法被解析。 |
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
渲染由給定的 PHP 語句返回的動態內容。
當內容的某些部分 (稱為動態內容) 不應被快取時,此方法主要與內容快取 (片段快取和頁面快取) 一起使用。動態內容必須由一些 PHP 語句回傳。
public string renderDynamic ( $statements ) | ||
由 setter 定義的屬性:將屬性值設定為 null | string |
行為的屬性:將屬性值設定為 null |
傳遞給方法的參數 | string |
動態內容的佔位符,或者如果目前沒有活動的內容快取,則為動態內容。 請注意,大多數間接修改版面的方法,例如 registerJS() 或 registerJSFile(),不適用於動態呈現。 |
---|
public function renderDynamic($statements)
{
if (!empty($this->cacheStack)) {
$n = count($this->dynamicPlaceholders);
$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
$this->addDynamicPlaceholder($placeholder, $statements);
return $placeholder;
}
return $this->evaluateDynamicContent($statements);
}
呈現視圖檔案。
如果 $theme 已啟用 (非 null),它將嘗試呈現視圖檔案的主題版本,只要該版本可用。
此方法將會調用 yii\helpers\FileHelper::localize() 來本地化視圖檔案。
如果 renderer 已啟用 (非 null),此方法將會使用它來呈現視圖檔案。否則,它將只會將視圖檔案作為普通的 PHP 檔案包含進來,捕獲其輸出並將其作為字串回傳。
public string renderFile ( $viewFile, $params = [], $context = null ) | ||
$viewFile | string |
視圖檔案。這可以是絕對檔案路徑或其別名。 |
$params | array |
將被提取並在視圖檔案中提供的參數 (名稱-值 對)。 |
$context | object|null |
視圖在呈現視圖時應使用的上下文。如果為 null,將會使用現有的 $context。 |
傳遞給方法的參數 | string |
呈現結果 |
---|---|---|
方法傳回值 | yii\base\ViewNotFoundException |
如果視圖檔案不存在 |
public function renderFile($viewFile, $params = [], $context = null)
{
$viewFile = $requestedFile = Yii::getAlias($viewFile);
if ($this->theme !== null) {
$viewFile = $this->theme->applyTo($viewFile);
}
if (is_file($viewFile)) {
$viewFile = FileHelper::localize($viewFile);
} else {
throw new ViewNotFoundException("The view file does not exist: $viewFile");
}
$oldContext = $this->context;
if ($context !== null) {
$this->context = $context;
}
$output = '';
$this->_viewFiles[] = [
'resolved' => $viewFile,
'requested' => $requestedFile
];
if ($this->beforeRender($viewFile, $params)) {
Yii::debug("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) {
if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
/* @var $renderer ViewRenderer */
$renderer = $this->renderers[$ext];
$output = $renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
$this->afterRender($viewFile, $params, $output);
}
array_pop($this->_viewFiles);
$this->context = $oldContext;
return $output;
}
將視圖檔案呈現為 PHP 腳本。
這個方法將視圖檔案視為 PHP 腳本並包含該檔案。它會提取給定的參數,並使它們在視圖檔案中可用。這個方法會捕獲包含的視圖檔案的輸出,並將其作為字串返回。
這個方法主要應由視圖渲染器或 renderFile() 呼叫。
public string renderPhpFile ( $_file_, $_params_ = [] ) | ||
$_file_ | string |
視圖檔案。 |
$_params_ | array |
將被提取並在視圖檔案中提供的參數 (名稱-值 對)。 |
傳遞給方法的參數 | string |
呈現結果 |
---|---|---|
方法傳回值 | Throwable |
public function renderPhpFile($_file_, $_params_ = [])
{
$_obInitialLevel_ = ob_get_level();
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
try {
require $_file_;
return ob_get_clean();
} catch (\Exception $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $_obInitialLevel_) {
if (!@ob_end_clean()) {
ob_clean();
}
}
throw $e;
}
}
設定動態內容的佔位符清單。此方法在內部用於實作內容快取功能。
public void setDynamicPlaceholders ( $placeholders ) | ||
$placeholders | array |
佔位符號的列表。 |
public function setDynamicPlaceholders($placeholders)
{
$this->dynamicPlaceholders = $placeholders;
}
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);
}
事件詳細資訊
此事件在 renderFile() 呈現視圖檔案之後立即觸發。
此事件在 renderFile() 呈現視圖檔案之前立即觸發。
此事件由 beginPage() 觸發。
此事件由 endPage() 觸發。
註冊 或 登入 以發表評論。