Class yii\web\AssetBundle
AssetBundle 代表資源檔案的集合,例如 CSS、JS、圖片。
每個資源包都有一個唯一的名稱,可在應用程式中使用的所有資源包中全局識別它。 該名稱是代表它的類別的完整限定類別名稱。
一個資源包可以依賴其他資源包。 當向視圖註冊一個資源包時,所有其依賴的資源包將會自動註冊。
有關 AssetBundle 的更多詳細資訊和使用方法,請參閱關於資源的指南文章。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$basePath | string | 包含此資源包中資源檔案的可透過網路存取的目錄。 | yii\web\AssetBundle |
$baseUrl | string | 列於 $js 與 $css 中相關資源檔案的基礎 URL。 | yii\web\AssetBundle |
$css | array | 此資源包包含的 CSS 檔案列表。 | yii\web\AssetBundle |
$cssOptions | array | 在註冊此資源包中的 CSS 檔案時,將傳遞給 yii\web\View::registerCssFile() 的選項。 | yii\web\AssetBundle |
$depends | array | 此資源包依賴的資源包類別名稱列表。 | yii\web\AssetBundle |
$js | array | 此資源包包含的 JavaScript 檔案列表。 | yii\web\AssetBundle |
$jsOptions | array | 在註冊此資源包中的 JS 檔案時,將傳遞給 yii\web\View::registerJsFile() 的選項。 | yii\web\AssetBundle |
$publishOptions | array | 當發布資源包時,將傳遞給 yii\web\AssetManager::publish() 的選項。 | yii\web\AssetBundle |
$sourcePath | string|null | 包含此資源包原始資源檔案的目錄。 | yii\web\AssetBundle |
公開方法
方法 | 描述 | 定義於 |
---|---|---|
__call() | 呼叫指定的非類別方法。 | yii\base\BaseObject |
__construct() | 建構子。 | yii\base\BaseObject |
__get() | 傳回物件屬性的值。 | yii\base\BaseObject |
__isset() | 檢查屬性是否已設定,即已定義且非 null。 | yii\base\BaseObject |
__set() | 設定物件屬性的值。 | yii\base\BaseObject |
__unset() | 將物件屬性設定為 null。 | yii\base\BaseObject |
canGetProperty() | 傳回一個值,指示屬性是否可讀取。 | yii\base\BaseObject |
canSetProperty() | 傳回一個值,指示屬性是否可設定。 | yii\base\BaseObject |
className() | 傳回此類別的完整限定名稱。 | yii\base\BaseObject |
hasMethod() | 傳回一個值,指示方法是否已定義。 | yii\base\BaseObject |
hasProperty() | 傳回一個值,指示屬性是否已定義。 | yii\base\BaseObject |
init() | 初始化資源包。 | yii\web\AssetBundle |
publish() | 如果資源包的原始碼不在可透過網路存取的目錄下,則發布該資源包。 | yii\web\AssetBundle |
register() | 向視圖註冊此資源包。 | yii\web\AssetBundle |
registerAssetFiles() | 向給定的視圖註冊 CSS 和 JS 檔案。 | yii\web\AssetBundle |
屬性詳情
包含此資源包中資源檔案的可透過網路存取的目錄。
如果設定了 $sourcePath,當 yii\web\AssetManager 從 $sourcePath 發布資源檔案時,此屬性將被覆寫。
您可以使用目錄或目錄的別名。
如果設定了 $sourcePath,當 yii\web\AssetManager 從 $sourcePath 發布資源檔案時,此屬性將被覆寫。
您可以使用 URL 或 URL 的別名。
此資源包包含的 CSS 檔案列表。每個 CSS 檔案可以使用 $js 中解釋的三種格式之一指定。
請注意,目錄分隔符號應僅使用正斜線 "/"。
在註冊此資源包中的 CSS 檔案時,將傳遞給 yii\web\View::registerCssFile() 的選項。
此資源包依賴的資源包類別名稱列表。
例如
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
此資源包包含的 JavaScript 檔案列表。每個 JavaScript 檔案可以使用以下格式之一指定
- 代表外部資源的絕對 URL。例如,
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
或//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
。 - 代表本地資源的相對路徑(例如
js/main.js
)。本地資源的實際檔案路徑可以通過在相對路徑前加上 $basePath 來確定,而資源的實際 URL 可以通過在相對路徑前加上 $baseUrl 來確定。 - 一個陣列,其中第一個條目是如前所述的 URL 或相對路徑,以及將用於覆寫此條目的 $jsOptions 設定的鍵 => 值配對列表。此功能自 2.0.7 版本起可用。
請注意,目錄分隔符號應僅使用正斜線 "/"。
在註冊此資源包中的 JS 檔案時,將傳遞給 yii\web\View::registerJsFile() 的選項。
當資源包正在發佈時,要傳遞給 yii\web\AssetManager::publish() 的選項。僅當設定了 $sourcePath 時,才會使用此屬性。
包含此資源包的來源資源檔案的目錄。來源資源檔案是 Web 應用程式的原始碼儲存庫一部分的檔案。
如果包含來源資源檔案的目錄無法透過 Web 存取,則必須設定此屬性。透過設定此屬性,當資源包在頁面上註冊時,yii\web\AssetManager 將自動將來源資源檔案發佈到可透過 Web 存取的目錄。
如果您未設定此屬性,則表示來源資源檔案位於 $basePath 下。
您可以使用目錄或目錄的別名。
另請參閱 $publishOptions。
方法詳情
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作會執行兩件事
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果子類別中覆寫了此方法,建議
- 建構子的最後一個參數是一個組態陣列,就像此處的
$config
一樣。 - 在建構子的結尾呼叫父類別實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值配對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: yii\base\BaseObject::__get()
傳回物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property;
時,它會被隱式呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
$name |
return | mixed |
屬性名稱 |
---|---|---|
throws | 屬性值 |
yii\base\UnknownPropertyException |
throws | 如果未定義屬性 |
yii\base\InvalidCallException |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (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\BaseObject::__isset()
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property)
時,它會被隱式呼叫。
請注意,如果未定義屬性,將傳回 false。
另請參閱 https://php.dev.org.tw/manual/en/function.isset.php。 | ||
$name | string | |
return | $name |
屬性名稱或事件名稱 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式呼叫。 | ||
$name | string | |
另請參閱 __get()。 | mixed |
屬性名稱 |
throws | 屬性值 |
yii\base\UnknownPropertyException |
---|---|---|
throws | 如果未定義屬性 |
public void __set ( $name, $value ) |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
如果屬性為唯讀
將物件屬性設定為 null。
定義於: yii\base\BaseObject::__unset()
請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property)
時,它會被隱式呼叫。
請注意,如果未定義屬性,此方法將不執行任何操作。如果屬性為唯讀,它將拋出例外。 | ||
$name | string |
$name |
throws | 如果未定義屬性 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
$name
傳回一個值,指示屬性是否可讀取。
如果屬性為唯讀。
- canGetProperty() public 方法
- 定義於: yii\base\BaseObject::canGetProperty()
如果符合以下條件,則屬性為可讀取:
類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫); | ||
$name | string |
$name |
類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時); |
$name |
另請參閱 canSetProperty()。 |
return | $name |
public boolean canGetProperty ( $name, $checkVars = true ) |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
是否將成員變數視為屬性
傳回一個值,指示屬性是否可設定。
是否可以讀取屬性
- canSetProperty() public 方法
- 定義於: yii\base\BaseObject::canGetProperty()
如果符合以下條件,則屬性為可寫入: | ||
$name | string |
$name |
類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時); |
$name |
另請參閱 canSetProperty()。 |
return | $name |
類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫); |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
是否可以寫入屬性
傳回此類別的完整限定名稱。
className() public 靜態方法 | ||
return | string |
自 2.0.14 版本起已過時。在 PHP >=5.5 上,請改用 |
---|
public static function className()
{
return get_called_class();
}
hasMethod() public 方法 | ||
$name | string |
方法名稱 |
return | $name |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
method_exists()
的呼叫。當您實作 php 魔術方法 __call()
時,您可以覆寫此方法。public boolean hasMethod ( $name )
傳回一個值,指示屬性是否已定義。
$name
- 方法是否已定義
- 定義於: yii\base\BaseObject::canGetProperty()
定義於: yii\base\BaseObject::hasProperty() | ||
$name | string |
$name |
類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時); |
$name |
另請參閱 canSetProperty()。 |
return | $name |
如果符合以下條件,則定義了屬性: |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
初始化資源包。
另請參閱
public boolean hasProperty ( $name, $checkVars = true ) |
public function init()
{
if ($this->sourcePath !== null) {
$this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
}
if ($this->basePath !== null) {
$this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
}
if ($this->baseUrl !== null) {
$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
}
}
如果您覆寫此方法,請確保在最後呼叫父類別實作。 | ||
public void init ( ) | publish() public 方法 |
它也會嘗試使用資源轉換器將非 CSS 或 JS 檔案(例如 LESS、Sass)轉換為對應的 CSS 或 JS 檔案。 |
public function publish($am)
{
if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}
if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
foreach ($this->js as $i => $js) {
if (is_array($js)) {
$file = array_shift($js);
if (Url::isRelative($file)) {
$js = ArrayHelper::merge($this->jsOptions, $js);
array_unshift($js, $converter->convert($file, $this->basePath));
$this->js[$i] = $js;
}
} elseif (Url::isRelative($js)) {
$this->js[$i] = $converter->convert($js, $this->basePath);
}
}
foreach ($this->css as $i => $css) {
if (is_array($css)) {
$file = array_shift($css);
if (Url::isRelative($file)) {
$css = ArrayHelper::merge($this->cssOptions, $css);
array_unshift($css, $converter->convert($file, $this->basePath));
$this->css[$i] = $css;
}
} elseif (Url::isRelative($css)) {
$this->css[$i] = $converter->convert($css, $this->basePath);
}
}
}
}
向視圖註冊此資源包。
$am | ||
yii\web\AssetManager | 執行資源發佈的資源管理器 | |
return | yii\web\AssetBundle |
public static static register ( $view ) |
---|
public static function register($view)
{
return $view->registerAssetBundle(get_called_class());
}
向給定的視圖註冊 CSS 和 JS 檔案。
yii\web\View | ||
yii\web\AssetManager | 執行資源發佈的資源管理器 |
要註冊的視圖 |
public function registerAssetFiles($view)
{
$manager = $view->getAssetManager();
foreach ($this->js as $js) {
if (is_array($js)) {
$file = array_shift($js);
$options = ArrayHelper::merge($this->jsOptions, $js);
$view->registerJsFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
} elseif ($js !== null) {
$view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
}
}
foreach ($this->css as $css) {
if (is_array($css)) {
$file = array_shift($css);
$options = ArrayHelper::merge($this->cssOptions, $css);
$view->registerCssFile($manager->getAssetUrl($this, $file, ArrayHelper::getValue($options, 'appendTimestamp')), $options);
} elseif ($css !== null) {
$view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
}
}
}
public void registerAssetFiles ( $view )