问题描述
我只是使用 Yii 框架构建了一个 CMS 框架.我想将 CMS 部署到多个域.
I just build a CMS framework using Yii framework. I want to deploy the CMS to multiple domains.
/home/root/www/domain1.com
/home/root/www/domain2.com
/home/root/www/domain3.com
/home/root/www/domain4.com
我想重用css文件和受保护文件夹下的所有文件,这样一旦我更新了css和受保护文件夹中的文件,所有域都应该反映更改.
I want to reuse the css files and all the files under protected folder, so that once I update css and the files in the protected folder, all the domains should reflect the change.
推荐答案
是的,Yii 支持这个.事实上,这就是我配置一些网站的方式.
Yes, Yii supports this. In fact, this is how I have some websites configured.
(当然,前提是您的所有站点都在同一台服务器上.但我看到 Evan 有这个.这在跨服务器上是行不通的.)
(Of course, this is predicated on having all your sites on the same server. But I see that Evan has this. This would not work accross servers.)
首先,这将要求您将代码移出 Web 根目录并移至文档根目录中.请参阅此处一>.
Firstly, it would require that you move your code out of the web-root and into the document root. See here.
其次,它要求你使用 Yii AssetsBase.请参阅此处和那里.我发现资产管理需要配置(但使用起来很轻松).这就是我最终的结果:
Secondly, it requires that you use Yii AssetsBase. See here and there. I found the asset management a bear to configure (but a breeze to work with). This is what I ended up with:
在 components/Controller.php 中包含以下内容:
In components/Controller.php include the following:
/**
* @var registers which js, css, images have been published
* See: http://www.yiiframework.com/wiki/311/assetmanager-clearing-browser-s-cache-on-site- update/
*/
private $_assetsBase;
public function getAssetsBase()
{
if ($this->_assetsBase === null) {
Yii::app()->assetManager->newDirMode = 0755;
Yii::app()->assetManager->newFileMode = 0644;
$this->_assetsBase = Yii::app()->assetManager->publish(
Yii::getPathOfAlias('application.assets'),
false,
-1,
defined('YII_DEBUG') && YII_DEBUG
);
}
return $this->_assetsBase;
}
以上假设你的 JS、CSS 和图片位置如下:
The above presupposes that your JS, CSS and images are located as follows:
protected/assets/js/mobiscroll-2.3.custom.min.js
protected/assets/css/mobiscroll-2.3.custom.min.css
protected/assets/img/einstein.png
然后在您的视图中,按如下方式调用您的资产:
Then in your views, call your assets as follows:
<?php
$cs->registerScriptFile($this->assetsBase.'/js/mobiscroll-2.3.1/js/mobiscroll-2.3.custom.min.js');
$cs->registerCssFile($this->assetsBase.'/js/mobiscroll-2.3.1/css/mobiscroll-2.3.custom.min.css');
?>
<img src="<?php echo $this->assetsBase ?>/img/einstein.png">
最后,在您对 JS 或 CSS 进行更改后,您将希望在所有用户的浏览器中强制刷新缓存.您可以通过触摸(原始)资产目录来完成此操作.这将强制 Yii 重新散列(已发布的)资产目录.随后,您的 JS &CSS 将在所有用户的浏览器中刷新.做这样的事情:
Finally, after you have made changes to your JS or CSS, you will want to force a cache refresh in all users' browsers. You do this by touching the (original) assets directory. This will force Yii to rehash the (published) assets directory. Subsequently, your JS & CSS will be refreshed in all users' browsers. Do something like this:
$command = 'touch /path/to/your/website/protected/assets';
exec ( $command.' 2>&1', $output , $result );
if ($result === 0) {
$message = 'Assets have been pointed; a new directory should now be hashed';
} else {
$message = 'Looks like something went wrong. Assets not pointed?';
} // END if
这篇关于如何跨多个域重用代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!