クラス yii\widgets\DetailView
DetailView は、単一データの$modelの詳細を表示します。
DetailView は、モデルを通常の形式で表示するのに最適です(例:各モデル属性はテーブルの行として表示されます)。モデルはyii\base\Modelのインスタンス、または連想配列のいずれかです。
DetailView は、$attributesプロパティを使用して、どのモデル属性を表示し、どのようにフォーマットするかを決定します。
DetailView の一般的な使い方は次のとおりです。
echo DetailView::widget([
'model' => $model,
'attributes' => [
'title', // title attribute (in plain text)
'description:html', // description attribute in HTML
[ // the owner name of the model
'label' => 'Owner',
'value' => $model->owner->name,
],
'created_at:datetime', // creation date formatted as datetime
],
]);
DetailView の詳細と使用方法については、データウィジェットに関するガイド記事を参照してください。
公開プロパティ
プロパティ | 型 | 説明 | 定義元 |
---|---|---|---|
$attributes | 配列 | 詳細ビューに表示される属性のリスト。 | yii\widgets\DetailView |
$autoIdPrefix | 文字列 | 自動生成されたウィジェットIDへのプレフィックス。 | yii\base\Widget |
$behaviors | yii\base\Behavior[] | このコンポーネントにアタッチされている動作のリスト。 | yii\base\Component |
$formatter | 配列|yii\i18n\Formatter|null | モデル属性値を表示可能なテキストにフォーマットするために使用されるフォーマッタ。 | yii\widgets\DetailView |
$id | 文字列|null | ウィジェットのID。 | yii\base\Widget |
$model | 配列|オブジェクト | 詳細を表示するデータモデル。 | yii\widgets\DetailView |
$options | 配列|null | このウィジェットのコンテナタグのHTML属性。 | yii\widgets\DetailView |
$template | 文字列|callable | 単一属性のレンダリングに使用されるテンプレート。 | yii\widgets\DetailView |
$view | yii\web\View | ビューまたはビューファイルをレンダリングするために使用できるビューオブジェクト。 | yii\base\Widget |
$viewPath | 文字列 | このウィジェットのビューファイルを含むディレクトリ。 | yii\base\Widget |
公開メソッド
メソッド | 説明 | 定義元 |
---|---|---|
__call() | クラスメソッドではない名前付きメソッドを呼び出します。 | yii\base\Component |
__clone() | このメソッドは、既存のオブジェクトを複製してオブジェクトが作成された後に呼び出されます。 | yii\base\Component |
__construct() | コンストラクタ。 | yii\base\BaseObject |
__get() | コンポーネントプロパティの値を返します。 | yii\base\Component |
__isset() | プロパティが設定されているか(つまり、定義されていてnullではないか)を確認します。 | yii\base\Component |
__set() | コンポーネントプロパティの値を設定します。 | yii\base\Component |
__unset() | コンポーネントプロパティをnullに設定します。 | yii\base\Component |
afterRun() | このメソッドは、ウィジェットが実行された直後に呼び出されます。 | yii\base\Widget |
attachBehavior() | このコンポーネントに動作をアタッチします。 | yii\base\Component |
attachBehaviors() | コンポーネントに動作のリストをアタッチします。 | yii\base\Component |
beforeRun() | このメソッドは、ウィジェットが実行される直前に呼び出されます。 | yii\base\Widget |
begin() | ウィジェットを開始します。 | yii\base\Widget |
behaviors() | このコンポーネントが動作するべきビヘイビアのリストを返します。 | yii\base\Component |
canGetProperty() | プロパティが読み取り可能かどうかを示す値を返します。 | yii\base\Component |
canSetProperty() | プロパティが設定可能かどうかを示す値を返します。 | yii\base\Component |
className() | このクラスの完全修飾名を返します。 | yii\base\BaseObject |
detachBehavior() | コンポーネントからビヘイビアをデタッチします。 | yii\base\Component |
detachBehaviors() | コンポーネントからすべてのビヘイビアをデタッチします。 | yii\base\Component |
end() | ウィジェットを終了します。 | yii\base\Widget |
ensureBehaviors() | behaviors()で宣言されたビヘイビアがこのコンポーネントにアタッチされていることを確認します。 | yii\base\Component |
getBehavior() | 名前付きのビヘイビアオブジェクトを返します。 | yii\base\Component |
getBehaviors() | このコンポーネントにアタッチされているすべてのビヘイビアを返します。 | yii\base\Component |
getId() | ウィジェットのIDを返します。 | yii\base\Widget |
getView() | ビューまたはビューファイルをレンダリングするために使用できるビューオブジェクトを返します。 | yii\base\Widget |
getViewPath() | このウィジェットのビューファイルを含むディレクトリを返します。 | yii\base\Widget |
hasEventHandlers() | 名前付きイベントにハンドラがアタッチされているかどうかを示す値を返します。 | yii\base\Component |
hasMethod() | メソッドが定義されているかどうかを示す値を返します。 | yii\base\Component |
hasProperty() | このコンポーネントにプロパティが定義されているかどうかを示す値を返します。 | yii\base\Component |
init() | 詳細ビューを初期化します。 | yii\widgets\DetailView |
off() | このコンポーネントから既存のイベントハンドラをデタッチします。 | yii\base\Component |
on() | イベントにイベントハンドラをアタッチします。 | yii\base\Component |
render() | ビューをレンダリングします。 | yii\base\Widget |
renderFile() | ビューファイルをレンダリングします。 | yii\base\Widget |
run() | 詳細ビューをレンダリングします。 | yii\widgets\DetailView |
setId() | ウィジェットのIDを設定します。 | yii\base\Widget |
setView() | このウィジェットで使用されるビューオブジェクトを設定します。 | yii\base\Widget |
trigger() | イベントをトリガーします。 | yii\base\Component |
widget() | ウィジェットインスタンスを作成して実行します。 | yii\base\Widget |
保護されたメソッド
メソッド | 説明 | 定義元 |
---|---|---|
normalizeAttributes() | 属性の仕様を正規化します。 | yii\widgets\DetailView |
renderAttribute() | 単一の属性をレンダリングします。 | yii\widgets\DetailView |
イベント
イベント | 型 | 説明 | 定義元 |
---|---|---|---|
EVENT_AFTER_RUN | yii\base\WidgetEvent | ウィジェットの実行直後に発生するイベント。(バージョン2.0.11から利用可能) | yii\base\Widget |
EVENT_BEFORE_RUN | yii\base\WidgetEvent | ウィジェットの実行直前に発生するイベント。(バージョン2.0.11から利用可能) | yii\base\Widget |
EVENT_INIT | yii\base\Event | init()を介してウィジェットが初期化されたときにトリガーされるイベント。(バージョン2.0.11から利用可能) | yii\base\Widget |
プロパティの詳細
詳細ビューに表示される属性のリスト。各配列要素は、特定の属性の表示に関する仕様を表します。
属性は、`attribute`、`attribute:format`、または`attribute:format:label`という形式の文字列として指定できます。ここで、`attribute`は属性名を表し、`format`は属性の形式を表します。`format`はyii\i18n\Formatter::format()メソッドに渡され、属性値を表示可能なテキストにフォーマットします。サポートされている型については、yii\i18n\Formatterを参照してください。`format`と`label`はどちらもオプションです。存在しない場合はデフォルト値が使用されます。
属性は、以下の要素を持つ配列としても指定できます。
attribute
: 属性名。`label`または`value`が指定されていない場合、これは必須です。label
: 属性に関連付けられたラベル。これが指定されていない場合、属性名から生成されます。value
: 表示される値。これが指定されていない場合、$modelから属性名を使用してyii\helpers\ArrayHelper::getValue()を呼び出すことで取得されます。この値は、`format`オプションに従って表示可能なテキストにフォーマットされます。バージョン2.0.11以降、以下のパラメータを持つクロージャとして定義できます。function ($model, $widget)
$model
は表示されるモデルを参照し、$widget
はDetailView
ウィジェットのインスタンスです。format
: 値の型で、値を表示可能なテキストにどのようにフォーマットするかを決定します。サポートされている型についてはyii\i18n\Formatterを、この値の指定方法についてはyii\i18n\Formatter::format()を参照してください。visible
: 属性が表示可能かどうか。`false`に設定されている場合、属性は表示されません。contentOptions
: 値タグをカスタマイズするためのHTML属性。例:`['class' => 'bg-red']`。サポートされている構文については、yii\helpers\BaseHtml::renderTagAttributes()を参照してください。captionOptions
: ラベルタグをカスタマイズするためのHTML属性。例:`['class' => 'bg-red']`。サポートされている構文については、yii\helpers\BaseHtml::renderTagAttributes()を参照してください。
モデル属性値を表示可能なテキストにフォーマットするために使用されるフォーマッタ。yii\i18n\Formatterのインスタンス、またはyii\i18n\Formatterインスタンスを作成するための設定配列のいずれかになります。このプロパティが設定されていない場合、`formatter`アプリケーションコンポーネントが使用されます。
詳細を表示するデータモデル。yii\base\Modelインスタンス、連想配列、yii\base\Arrayableインターフェースを実装するオブジェクト、または公開アクセス可能な非静的プロパティが定義されたオブジェクトです。
このウィジェットのコンテナタグのHTML属性。`tag`オプションは、どのコンテナタグを使用するかを指定します。設定されていない場合は、デフォルトで`table`になります。
属性のレンダリング方法の詳細については、yii\helpers\Html::renderTagAttributes()も参照してください。
単一の属性をレンダリングするために使用されるテンプレート。文字列の場合、トークン`{label}`と`{value}`は、対応する属性のラベルと値に置き換えられます。コールバック(例:無名関数)の場合、シグネチャは次のようになります。
function ($attribute, $index, $widget)
ここで、`$attribute`はレンダリングされる属性の仕様を参照し、`$index`は$attributes配列における属性の0ベースのインデックスであり、`$widget`はこのウィジェットインスタンスを参照します。
バージョン2.0.10以降、ラベルと値のHTMLコンテナ要素のHTML属性を表す`{captionOptions}`と`{contentOptions}`トークンが利用可能です。
メソッドの詳細
定義: yii\base\Component::__call()
クラスメソッドではない名前付きメソッドを呼び出します。
このメソッドは、アタッチされたビヘイビアに指定されたメソッドが存在するかどうかをチェックし、存在する場合は実行します。
これはPHPのマジックメソッドであり、未知のメソッドが呼び出された際に暗黙的に呼び出されるため、直接呼び出さないでください。
public mixed __call ( $name, $params ) | ||
$name | 文字列 |
メソッド名 |
$params | 配列 |
メソッドパラメータ |
戻り値 | mixed |
メソッドの戻り値 |
---|---|---|
例外 | 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()");
}
定義: yii\base\Component::__clone()
このメソッドは、既存のオブジェクトを複製してオブジェクトが作成された後に呼び出されます。
古いオブジェクトにアタッチされているため、すべてのビヘイビアを削除します。
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義: yii\base\BaseObject::__construct()
コンストラクタ。
デフォルトの実装では、次の2つのことを行います。
- 指定された設定 `$config` を使用してオブジェクトを初期化します。
- init() を呼び出します。
このメソッドを子クラスでオーバーライドする場合は、
- コンストラクタの最後のパラメータが、ここにある `$config` のような設定配列であることをお勧めします。
- コンストラクタの最後に親の実装を呼び出してください。
public void __construct ( $config = [] ) | ||
$config | 配列 |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義: yii\base\Component::__get()
コンポーネントプロパティの値を返します。
このメソッドは、以下の順序でチェックし、それに応じて動作します。
- ゲッターによって定義されたプロパティ: ゲッターの結果を返します。
- ビヘイビアのプロパティ: ビヘイビアのプロパティ値を返します。
これはPHPのマジックメソッドであり、`$value = $component->property;` を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
__set() も参照してください。
public mixed __get ( $name ) | ||
$name | 文字列 |
プロパティ名 |
戻り値 | mixed |
プロパティ値、またはビヘイビアのプロパティの値 |
---|---|---|
例外 | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
例外 | 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ではないか)を確認します。
このメソッドは、以下の順序でチェックし、それに応じて動作します。
- セッターによって定義されたプロパティ: プロパティが設定されているかどうかを返します。
- ビヘイビアのプロパティ: プロパティが設定されているかどうかを返します。
- 存在しないプロパティの場合は `false` を返します。
これはPHPのマジックメソッドであり、`isset($component->property)` を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
public boolean __isset ( $name ) | ||
$name | 文字列 |
プロパティ名またはイベント名 |
戻り値 | 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()
コンポーネントプロパティの値を設定します。
このメソッドは、以下の順序でチェックし、それに応じて動作します。
- セッターによって定義されたプロパティ: プロパティ値を設定します。
- "on xyz"形式のイベント: "xyz"イベントにハンドラーをアタッチします。
- "as xyz"形式のビヘイビア: "xyz"という名前のビヘイビアをアタッチします。
- ビヘイビアのプロパティ: ビヘイビアのプロパティ値を設定します。
これはPHPのマジックメソッドであり、`$component->property = $value;` を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
__get() も参照してください。
public void __set ( $name, $value ) | ||
$name | 文字列 |
プロパティ名またはイベント名 |
$value | mixed |
プロパティ値 |
例外 | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
---|---|---|
例外 | 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に設定します。
このメソッドは、以下の順序でチェックし、それに応じて動作します。
- セッターによって定義されたプロパティ: プロパティ値をnullに設定します。
- ビヘイビアのプロパティ: プロパティ値をnullに設定します。
これはPHPのマジックメソッドであり、`unset($component->property)` を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
public void __unset ( $name ) | ||
$name | 文字列 |
プロパティ名 |
例外 | 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\Widget::afterRun()
このメソッドは、ウィジェットが実行された直後に呼び出されます。
このメソッドは、EVENT_AFTER_RUN イベントをトリガーします。メソッドの戻り値は、ウィジェットの戻り値として使用されます。
このメソッドをオーバーライドする場合は、コードを次のようにする必要があります。
public function afterRun($result)
{
$result = parent::afterRun($result);
// your custom code here
return $result;
}
public mixed afterRun ( $result ) | ||
$result | mixed |
ウィジェットの戻り値。 |
戻り値 | mixed |
処理されたウィジェットの結果。 |
---|
public function afterRun($result)
{
$event = new WidgetEvent();
$event->result = $result;
$this->trigger(self::EVENT_AFTER_RUN, $event);
return $event->result;
}
定義: yii\base\Component::attachBehavior()
このコンポーネントに動作をアタッチします。
このメソッドは、指定された設定に基づいてビヘイビアオブジェクトを作成します。その後、yii\base\Behavior::attach() メソッドを呼び出すことで、ビヘイビアオブジェクトがこのコンポーネントにアタッチされます。
detachBehavior() も参照してください。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | 文字列 |
ビヘイビアの名前。 |
$behavior | string|array|yii\base\Behavior |
ビヘイビアの設定。以下のいずれかになります。
|
戻り値 | 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 | 配列 |
コンポーネントにアタッチするビヘイビアのリスト |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義場所: yii\base\Widget::beforeRun()
このメソッドは、ウィジェットが実行される直前に呼び出されます。
このメソッドは、EVENT_BEFORE_RUN イベントをトリガーします。メソッドの戻り値によって、ウィジェットの実行を続行するかどうかが決まります。
このメソッドをオーバーライドする場合は、次のように親の実装を呼び出すようにしてください。
public function beforeRun()
{
if (!parent::beforeRun()) {
return false;
}
// your custom code here
return true; // or false to not run the widget
}
public boolean beforeRun ( ) | ||
戻り値 | boolean |
ウィジェットの実行を続行するかどうか。 |
---|
public function beforeRun()
{
$event = new WidgetEvent();
$this->trigger(self::EVENT_BEFORE_RUN, $event);
return $event->isValid;
}
定義場所: yii\base\Widget::begin()
ウィジェットを開始します。
このメソッドは、呼び出し元のクラスのインスタンスを作成します。作成されたインスタンスに設定が適用されます。end() の対応する呼び出しを後で呼び出す必要があります。一部のウィジェットでは出力バッファリングを使用しているため、出力バッファのネストを中断しないように、同じビューで end() を呼び出す必要があります。
こちらも参照してください end().
public static static begin ( $config = [] ) | ||
$config | 配列 |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
戻り値 | yii\base\Widget |
新しく作成されたウィジェットインスタンス |
---|
public static function begin($config = [])
{
$config['class'] = get_called_class();
/* @var $widget Widget */
$widget = Yii::createObject($config);
self::$stack[] = $widget;
return $widget;
}
定義場所: yii\base\Component::behaviors()
このコンポーネントが動作するべきビヘイビアのリストを返します。
子クラスはこのメソッドをオーバーライドして、動作させたいビヘイビアを指定できます。
このメソッドの戻り値は、ビヘイビア名でインデックス付けされたビヘイビアオブジェクトまたは設定の配列である必要があります。ビヘイビアの設定は、ビヘイビアクラスを指定する文字列、または以下の構造の配列のいずれかです。
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
ビヘイビアクラスは yii\base\Behavior から拡張する必要があることに注意してください。ビヘイビアは、名前付きまたは匿名でアタッチできます。配列キーとして名前を使用すると、この名前を使用して、後で getBehavior() を使用してビヘイビアを取得したり、detachBehavior() を使用してビヘイビアをデタッチしたりできます。匿名ビヘイビアは取得またはデタッチできません。
このメソッドで宣言されたビヘイビアは、コンポーネントに自動的に(オンデマンドで)アタッチされます。
public array behaviors ( ) | ||
戻り値 | 配列 |
ビヘイビアの設定。 |
---|
public function behaviors()
{
return [];
}
定義場所: yii\base\Component::canGetProperty()
プロパティが読み取り可能かどうかを示す値を返します。
プロパティは、以下の場合に読み取ることができます。
- クラスに、指定された名前と関連付けられたゲッターメソッドがある場合(この場合、プロパティ名は区別されません)。
- クラスに、指定された名前のメンバ変数がある場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアに、指定された名前の読み取り可能なプロパティがある場合(
$checkBehaviors
が true の場合)。
こちらも参照してください canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 文字列 |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか。 |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか。 |
戻り値 | 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()
プロパティが設定可能かどうかを示す値を返します。
プロパティは、以下の場合に書き込むことができます。
- クラスに、指定された名前と関連付けられたセッターメソッドがある場合(この場合、プロパティ名は区別されません)。
- クラスに、指定された名前のメンバ変数がある場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアに、指定された名前の書き込み可能なプロパティがある場合(
$checkBehaviors
が true の場合)。
こちらも参照してください canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 文字列 |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか。 |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか。 |
戻り値 | 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 ( ) | ||
戻り値 | 文字列 |
このクラスの完全修飾名。 |
---|
public static function className()
{
return get_called_class();
}
定義場所: yii\base\Component::detachBehavior()
コンポーネントからビヘイビアをデタッチします。
ビヘイビアの yii\base\Behavior::detach() メソッドが呼び出されます。
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | 文字列 |
ビヘイビアの名前。 |
戻り値 | 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);
}
}
public static static end ( ) | ||
戻り値 | yii\base\Widget |
終了されたウィジェットインスタンス。 |
---|---|---|
例外 | yii\base\InvalidCallException |
public static function end()
{
if (!empty(self::$stack)) {
$widget = array_pop(self::$stack);
$calledClass = get_called_class();
if (Yii::$container->has($calledClass) && isset(Yii::$container->getDefinitions()[$calledClass]['class'])) {
$calledClass = Yii::$container->getDefinitions()[$calledClass]['class'];
}
if (get_class($widget) === $calledClass) {
/* @var $widget Widget */
if ($widget->beforeRun()) {
$result = $widget->run();
$result = $widget->afterRun($result);
echo $result;
}
return $widget;
}
throw new InvalidCallException('Expecting end() of ' . get_class($widget) . ', found ' . get_called_class());
}
throw new InvalidCallException('Unexpected ' . get_called_class() . '::end() call. A matching begin() is not found.');
}
定義されている場所: 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);
}
}
}
定義されている場所: yii\base\Component::getBehavior()
名前付きのビヘイビアオブジェクトを返します。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | 文字列 |
ビヘイビア名 |
戻り値 | 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 ( ) | ||
戻り値 | yii\base\Behavior[] |
このコンポーネントにアタッチされているビヘイビアのリスト |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
定義されている場所: yii\base\Widget::getId()
ウィジェットのIDを返します。
public string|null getId ( $autoGenerate = true ) | ||
$autoGenerate | boolean |
以前に設定されていない場合にIDを生成するかどうか |
戻り値 | 文字列|null |
ウィジェットのID。 |
---|
public function getId($autoGenerate = true)
{
if ($autoGenerate && $this->_id === null) {
$this->_id = static::$autoIdPrefix . static::$counter++;
}
return $this->_id;
}
定義されている場所: yii\base\Widget::getView()
ビューまたはビューファイルをレンダリングするために使用できるビューオブジェクトを返します。
render() と renderFile() メソッドは、このビューオブジェクトを使用して実際のビューレンダリングを実行します。設定されていない場合は、デフォルトで「view」アプリケーションコンポーネントになります。
public yii\web\View getView ( ) | ||
戻り値 | yii\web\View |
ビューまたはビューファイルをレンダリングするために使用できるビューオブジェクト。 |
---|
public function getView()
{
if ($this->_view === null) {
$this->_view = Yii::$app->getView();
}
return $this->_view;
}
定義されている場所: yii\base\Widget::getViewPath()
このウィジェットのビューファイルを含むディレクトリを返します。
デフォルトの実装では、ウィジェットのクラスファイルを含むディレクトリの下にある 'views' サブディレクトリが返されます。
public string getViewPath ( ) | ||
戻り値 | 文字列 |
このウィジェットのビューファイルを含むディレクトリ。 |
---|
public function getViewPath()
{
$class = new ReflectionClass($this);
return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
}
定義されている場所: yii\base\Component::hasEventHandlers()
名前付きイベントにハンドラがアタッチされているかどうかを示す値を返します。
public boolean hasEventHandlers ( $name ) | ||
$name | 文字列 |
イベント名 |
戻り値 | 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 | 文字列 |
プロパティ名 |
$checkBehaviors | boolean |
ビヘイビアのメソッドをこのコンポーネントのメソッドとして扱うかどうか |
戻り値 | 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()
このコンポーネントにプロパティが定義されているかどうかを示す値を返します。
プロパティは、以下の場合に定義されます。
- クラスが指定された名前と関連付けられたゲッターまたはセッターメソッドを持っている場合 (この場合、プロパティ名はケースインセンシティブです)。
- クラスに、指定された名前のメンバ変数がある場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアが指定された名前のプロパティを持っている場合 (
$checkBehaviors
が true の場合)。
こちらも参照してください
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 文字列 |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか。 |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか。 |
戻り値 | 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();
if ($this->model === null) {
throw new InvalidConfigException('Please specify the "model" property.');
}
if ($this->formatter === null) {
$this->formatter = Yii::$app->getFormatter();
} elseif (is_array($this->formatter)) {
$this->formatter = Yii::createObject($this->formatter);
}
if (!$this->formatter instanceof Formatter) {
throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');
}
$this->normalizeAttributes();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
}
属性の仕様を正規化します。
protected void normalizeAttributes ( ) | ||
例外 | yii\base\InvalidConfigException |
---|
protected function normalizeAttributes()
{
if ($this->attributes === null) {
if ($this->model instanceof Model) {
$this->attributes = $this->model->attributes();
} elseif (is_object($this->model)) {
$this->attributes = $this->model instanceof Arrayable ? array_keys($this->model->toArray()) : array_keys(get_object_vars($this->model));
} elseif (is_array($this->model)) {
$this->attributes = array_keys($this->model);
} else {
throw new InvalidConfigException('The "model" property must be either an array or an object.');
}
sort($this->attributes);
}
foreach ($this->attributes as $i => $attribute) {
if (is_string($attribute)) {
if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $attribute, $matches)) {
throw new InvalidConfigException('The attribute must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
}
$attribute = [
'attribute' => $matches[1],
'format' => isset($matches[3]) ? $matches[3] : 'text',
'label' => isset($matches[5]) ? $matches[5] : null,
];
}
if (!is_array($attribute)) {
throw new InvalidConfigException('The attribute configuration must be an array.');
}
if (isset($attribute['visible']) && !$attribute['visible']) {
unset($this->attributes[$i]);
continue;
}
if (!isset($attribute['format'])) {
$attribute['format'] = 'text';
}
if (isset($attribute['attribute'])) {
$attributeName = $attribute['attribute'];
if (!isset($attribute['label'])) {
$attribute['label'] = $this->model instanceof Model ? $this->model->getAttributeLabel($attributeName) : Inflector::camel2words($attributeName, true);
}
if (!array_key_exists('value', $attribute)) {
$attribute['value'] = ArrayHelper::getValue($this->model, $attributeName);
}
} elseif (!isset($attribute['label']) || !array_key_exists('value', $attribute)) {
throw new InvalidConfigException('The attribute configuration requires the "attribute" element to determine the value and display label.');
}
if ($attribute['value'] instanceof \Closure) {
$attribute['value'] = call_user_func($attribute['value'], $this->model, $this);
}
$this->attributes[$i] = $attribute;
}
}
定義されている場所: yii\base\Component::off()
このコンポーネントから既存のイベントハンドラをデタッチします。
このメソッドは、on() の反対です。
注: イベント名にワイルドカードパターンが渡された場合、このワイルドカードで登録されたハンドラーのみが削除され、このワイルドカードと一致するプレーン名で登録されたハンドラーは残ります。
こちらも参照してください on().
public boolean off ( $name, $handler = null ) | ||
$name | 文字列 |
イベント名 |
$handler | 呼び出し可能callable | null |
削除するイベントハンドラ。null の場合、指定されたイベントにアタッチされているすべてのハンドラが削除されます。 |
戻り値 | 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;
}
定義されている場所: 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 | 文字列 |
イベント名 |
$handler | 呼び出し可能 |
イベントハンドラ |
$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]);
}
}
定義されている場所: yii\base\Widget::render()
ビューをレンダリングします。
レンダリングするビューは、以下のいずれかの形式で指定できます。
- パスエイリアス (例: "@app/views/site/index")
- アプリケーション内の絶対パス (例: "//site/index"): ビュー名がダブルスラッシュで始まる場合。実際のビューファイルは、アプリケーションの ビューパス で検索されます。
- モジュール内の絶対パス (例: "/site/index"): ビュー名がシングルスラッシュで始まる場合。実際のビューファイルは、現在アクティブなモジュールの ビューパス で検索されます。
- 相対パス (例: "index"): 実際のビューファイルは $viewPath で検索されます。
ビュー名にファイル拡張子が含まれていない場合、デフォルトの .php
が使用されます。
public string render ( $view, $params = [] ) | ||
$view | 文字列 |
ビュー名。 |
$params | 配列 |
ビューで使用可能にするパラメータ(名前と値のペア)。 |
戻り値 | 文字列 |
レンダリング結果。 |
---|---|---|
例外 | yii\base\InvalidArgumentException |
ビューファイルが存在しない場合。 |
public function render($view, $params = [])
{
return $this->getView()->render($view, $params, $this);
}
単一の属性をレンダリングします。
protected string renderAttribute ( $attribute, $index ) | ||
$attribute | 配列 |
レンダリングする属性の仕様。 |
$index | 整数 |
$attributes 配列における属性のゼロベースインデックス。 |
戻り値 | 文字列 |
レンダリング結果 |
---|
protected function renderAttribute($attribute, $index)
{
if (is_string($this->template)) {
$captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', []));
$contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', []));
return strtr($this->template, [
'{label}' => $attribute['label'],
'{value}' => $this->formatter->format($attribute['value'], $attribute['format']),
'{captionOptions}' => $captionOptions,
'{contentOptions}' => $contentOptions,
]);
}
return call_user_func($this->template, $attribute, $index, $this);
}
定義されている場所: yii\base\Widget::renderFile()
ビューファイルをレンダリングします。
public string renderFile ( $file, $params = [] ) | ||
$file | 文字列 |
レンダリングするビューファイル。ファイルパスまたは パスエイリアス のいずれかです。 |
$params | 配列 |
ビューで使用可能にするパラメータ(名前と値のペア)。 |
戻り値 | 文字列 |
レンダリング結果。 |
---|---|---|
例外 | yii\base\InvalidArgumentException |
ビューファイルが存在しない場合。 |
public function renderFile($file, $params = [])
{
return $this->getView()->renderFile($file, $params, $this);
}
詳細ビューをレンダリングします。
これは、詳細ビューレンダリング全体のメインエントリです。
public void run ( ) |
public function run()
{
$rows = [];
$i = 0;
foreach ($this->attributes as $attribute) {
$rows[] = $this->renderAttribute($attribute, $i++);
}
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'table');
echo Html::tag($tag, implode("\n", $rows), $options);
}
定義されている場所: yii\base\Widget::setId()
ウィジェットのIDを設定します。
public void setId ( $value ) | ||
$value | 文字列 |
ウィジェットのID。 |
public function setId($value)
{
$this->_id = $value;
}
定義されている場所: yii\base\Widget::setView()
このウィジェットで使用されるビューオブジェクトを設定します。
public void setView ( $view ) | ||
$view | yii\base\View |
ビューまたはビューファイルをレンダリングするために使用できるビューオブジェクト。 |
public function setView($view)
{
$this->_view = $view;
}
定義されている場所: yii\base\Component::trigger()
イベントをトリガーします。
このメソッドは、イベントの発生を表します。クラスレベルのハンドラを含む、イベントにアタッチされているすべてのハンドラを呼び出します。
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | 文字列 |
イベント名 |
$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);
}
public static string widget ( $config = [] ) | ||
$config | 配列 |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
戻り値 | 文字列 |
ウィジェットのレンダリング結果。 |
---|---|---|
例外 | スロー可能 |
public static function widget($config = [])
{
ob_start();
ob_implicit_flush(false);
try {
/* @var $widget Widget */
$config['class'] = get_called_class();
$widget = Yii::createObject($config);
$out = '';
if ($widget->beforeRun()) {
$result = $widget->run();
$out = $widget->afterRun($result);
}
} catch (\Exception $e) {
// close the output buffer opened above if it has not been closed already
if (ob_get_level() > 0) {
ob_end_clean();
}
throw $e;
} catch (\Throwable $e) {
// close the output buffer opened above if it has not been closed already
if (ob_get_level() > 0) {
ob_end_clean();
}
throw $e;
}
return ob_get_clean() . $out;
}
コメントするには、サインアップするかログインしてください。