php Yii2 资产清除缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24819220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 assets clear cache
提问by Ruben
Everytime I update my css or js files in infoweb\menu\module\assets, I have to empty the backend\web\assets folder
is there a way to automatically clear the assets cache?
每次更新 infoweb\menu\module\assets 中的 css 或 js 文件时,都必须清空 backend\web\assets 文件夹
,有没有办法自动清除资产缓存?
采纳答案by Ruben
Add this in your view:
在您的视图中添加以下内容:
use vendor\myVendorName\myPackageName\assets\AppAsset;
AppAsset::register($this);
Add this in your config:
在你的配置中添加这个:
'components' => [
'assetManager' => [
'linkAssets' => true,
],
]
Empty assets folder, and refresh, done
清空资产文件夹,并刷新,完成
回答by Denis Rudov
there is additional property as
有额外的财产作为
if (YII_ENV_DEV) {
...;
...;
...;
$config['components']['assetManager']['forceCopy'] = true;
...;
...;
}
to publish files even there are published before
发布文件,即使之前已发布
回答by Brand Guy
If you enviroment is production I recommend to use Cache Busting:
如果您的环境是生产环境,我建议使用Cache Busting:
return [
// ...
'components' => [
'assetManager' => [
'appendTimestamp' => true,
],
],
];
for more information about assets, read the Assets Yii2 documentation.
有关资产的更多信息,请阅读资产 Yii2 文档。
回答by grigson
If you are developing your own plugin, you can force publish assets per bundle (note: $sourcePath should be set)
如果你正在开发自己的插件,你可以强制每个包发布资产(注意:$sourcePath 应该被设置)
<?php
namespace app\components\forms\redactorAssets;
use yii\web\AssetBundle;
class RedactorCutAsset extends AssetBundle {
public $sourcePath = '@app/components/forms/redactorAssets/assets';
public $js = [
'cut.js',
];
public $publishOptions = [
'forceCopy'=>true,
];
}
回答by Andrey Bessonov
sudo rm -rf frontend/web/assets/*
sudo chmod 777 frontend/web/assets
./yii cache/flush-all
If this does not work:
如果这不起作用:
sudo rm -rf vendor/*
composer install
回答by itnelo
I use CClientScript::registerScriptFile
method in my view files:
我CClientScript::registerScriptFile
在视图文件中使用方法:
Yii::app()->clientScript->registerScriptFile(
$this->getAssetsBase() . '/js/script.js'
);
If I modified script.js, after next page reload I will see all changes
如果我修改了 script.js,在下一页重新加载后,我将看到所有更改
For css files - CClientScript::registerCssFile
对于 css 文件 - CClientScript::registerCssFile
Yii::app()->clientScript->registerCssFile(
$this->getAssetsBase() . '/css/style.css'
);
UPDATE: if you use yii 2.0 beta, you can read some information about changes in mechanic of client helpers here: link
更新:如果您使用 yii 2.0 beta,您可以在此处阅读有关客户端助手机制更改的一些信息: link
回答by marvin_x
The AssetManager will create a hash based on the file modification time. The modification time of a directory does not change when any file is changed.
If you have an AssetBundle that has a directory as $sourcePath
, the modification time of the directory is used, the hash will not change, and nothing gets copied to the web/assets
directory.
AssetManager 将根据文件修改时间创建一个哈希值。修改任何文件时,目录的修改时间不会改变。如果您的 AssetBundle 的目录为 as $sourcePath
,则使用目录的修改时间,哈希不会更改,并且不会将任何内容复制到web/assets
目录中。
I suggest overriding AssetManager::hash()
in a subclass or write a function for AssetManager::$hashCallback
:
我建议AssetManager::hash()
在子类中覆盖或编写一个函数AssetManager::$hashCallback
:
'components' => [
'assetManager' => [
'hashCallback' => function($path) {
// if: $path is directory: figure out when files were changed in directory
// else: use original hash function in \yii\web\AssetManager
}
],
]
For a sample implementation for finding the max modified date over all asset files in a bundle you could look at this comment by wookie @ http://php.net/manual/en/function.filemtime.php#35779
有关查找包中所有资产文件的最大修改日期的示例实现,您可以查看 wookie @ http://php.net/manual/en/function.filemtime.php#35779 的此评论
Note that modification to any asset file will create a new directory in web/assets
, and regular cleanup will remain necessary. However, browser cache aside, refreshing the page will follow the latest changes.
请注意,对任何资产文件的修改都会在 中创建一个新目录web/assets
,并且仍然需要定期清理。但是,除了浏览器缓存之外,刷新页面将遵循最新的变化。
回答by Paul Huo
I configure the assetManager::forceCopy=true
in main-local.php for the dev environment like this
我assetManager::forceCopy=true
在 main-local.php 中为这样的开发环境配置了
return [
'components' => [
...
'assetManager' => [
'forceCopy' => true,
]
...
],
];