来自自定义资产中依赖项的 Yii2 位置
Yii2 location from dependencies in custom asset
我在前端有一个类似于 /actions/users/someaction 的路径,我想从后端使用 Bootstrap-Asset(位于 /backend/web/assets/xxxxxx/)。
所以我创建了一个名为 "ActionAsset" 的资产,内容如下:
class ActionAsset extends AssetBundle
{
public $basePath = '@backend';
public $baseUrl = '@web/backend';
public $css = [
'css/external.css',
'css/overwrite-bootstrap.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BackendBootstrapAsset',
];
}
包含的 css 工作正常,但依赖项始终保存在 /frontend/web/assets/ 中。
我的问题是(我真的搜索了几周)如何将此位置更改为 /backend/web/assets。
您需要定义$sourcePath
。 Yii2 AssetManager 将复制(或符号链接)您当前 web/assets/ 文件夹中的资产。
来自docs
sourcePath
: specifies the root directory that contains the asset files in this bundle. This property should be set if the root directory is not Web accessible. Otherwise, you should set the basePath property and baseUrl, instead. Path aliases can be used here.
因此将您的代码更改为:
class ActionAsset extends AssetBundle
{
public $sourcePath = '<path to your asste content>';
public $css = [
'css/external.css',
'css/overwrite-bootstrap.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BackendBootstrapAsset',
];
}
或者:只需在 @frontend/asset
中覆盖您的 asstetbundle 并相应地设置 $sourcePath
属性:
class FrontendActionAsset extends ActionAsset
{
public $sourcePath = '<path to your asste content>'; //you don't need more
}
资产发布
如前所述,如果资源包位于 Web 无法访问的目录中,则在向视图注册资源包时,其资源将被复制到 Web 目录中。此过程称为资产发布,由资产管理器自动完成。
默认情况下,资产发布到目录@webroot/assets,对应于URL @web/assets。您可以通过配置 basePath 和 baseUrl 属性来自定义此位置。
如果您的 OS 和 Web 服务器允许,您可以考虑使用符号 link,而不是通过文件复制来发布资产。可以通过将 linkAssets 设置为 true 来启用此功能。
return [
// ...
'components' => [
'assetManager' => [
'linkAssets' => true,
],
],
];
使用上述配置,资产管理器将在资产包发布时创建一个符号 link 到资产包的源路径。这比文件复制更快,也可以确保发布的资产始终是 up-to-date.
https://www.yiiframework.com/doc/guide/2.0/en/structure-assets
我在前端有一个类似于 /actions/users/someaction 的路径,我想从后端使用 Bootstrap-Asset(位于 /backend/web/assets/xxxxxx/)。
所以我创建了一个名为 "ActionAsset" 的资产,内容如下:
class ActionAsset extends AssetBundle
{
public $basePath = '@backend';
public $baseUrl = '@web/backend';
public $css = [
'css/external.css',
'css/overwrite-bootstrap.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BackendBootstrapAsset',
];
}
包含的 css 工作正常,但依赖项始终保存在 /frontend/web/assets/ 中。 我的问题是(我真的搜索了几周)如何将此位置更改为 /backend/web/assets。
您需要定义$sourcePath
。 Yii2 AssetManager 将复制(或符号链接)您当前 web/assets/ 文件夹中的资产。
来自docs
sourcePath
: specifies the root directory that contains the asset files in this bundle. This property should be set if the root directory is not Web accessible. Otherwise, you should set the basePath property and baseUrl, instead. Path aliases can be used here.
因此将您的代码更改为:
class ActionAsset extends AssetBundle
{
public $sourcePath = '<path to your asste content>';
public $css = [
'css/external.css',
'css/overwrite-bootstrap.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BackendBootstrapAsset',
];
}
或者:只需在 @frontend/asset
中覆盖您的 asstetbundle 并相应地设置 $sourcePath
属性:
class FrontendActionAsset extends ActionAsset
{
public $sourcePath = '<path to your asste content>'; //you don't need more
}
资产发布 如前所述,如果资源包位于 Web 无法访问的目录中,则在向视图注册资源包时,其资源将被复制到 Web 目录中。此过程称为资产发布,由资产管理器自动完成。
默认情况下,资产发布到目录@webroot/assets,对应于URL @web/assets。您可以通过配置 basePath 和 baseUrl 属性来自定义此位置。
如果您的 OS 和 Web 服务器允许,您可以考虑使用符号 link,而不是通过文件复制来发布资产。可以通过将 linkAssets 设置为 true 来启用此功能。
return [
// ...
'components' => [
'assetManager' => [
'linkAssets' => true,
],
],
];
使用上述配置,资产管理器将在资产包发布时创建一个符号 link 到资产包的源路径。这比文件复制更快,也可以确保发布的资产始终是 up-to-date.
https://www.yiiframework.com/doc/guide/2.0/en/structure-assets