使用多个配置来简化部署
高级应用模板为它的每一个应用使用不同的配置文件。
common
config
main.php
main-local.php
params.php
params-local.php
console
config
main.php
main-local.php
params.php
params-local.php
backend
config
main.php
main-local.php
params.php
params-local.php
frontend
config
main.php
main-local.php
params.php
params-local.php
每一个入口web/index.php
脚本合并自己配置文件的集合:
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-local.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
每一个config/main.php
文件和并参数:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
// ...
'params' => $params,
];
这个系统允许你配置应用中常用和特殊应用属性和组件。并且我们可以基于版本控制系统,存储缺省配置文件,并忽略所有的*-local.php
文件。
所有的本地文件模板在environments
文件夹中准备,当你在控制台中运行php init
,并选择一个needle环境,这个初始脚本复制相应的文件,并将它们放在目标文件夹中。
但是基础应用模板不包含敏捷配置系统,只提供如下文件:
config
console.php
web.php
db.php
params.php
尝试添加一个高级配置系统到yii2-app-basic
应用模板中。
准备
按照官方指南http://www.yiiframework.com/doc-2.0/guide-start-installation.html的描述,使用Composer包管理器创建一个新的yii2-app-basic
应用。
如何做...
- 创建
config/common.php
文件:
<?php
$params = array_merge(
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'basePath' => dirname(__DIR__),
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
],
'db' => [],
],
'params' => $params,
];
- 创建
config/common-local
文件:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'mailer' => [
'useFileTransport' => true,
],
],
];
- 移除
config/db.php
文件: - 从
config/console.php
移除重复的代码:
<?php
Yii::setAlias('@tests', dirname(__DIR__) . '/tests');
return [
'id' => 'basic-console',
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
];
- 创建一个带有空数组的文件
config/console-local.php
:
<?php return [];
- 修改
config/web.php
文件:
$config = [
'id' => 'basic',
'bootstrap' => ['log'],
'components' => [
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
- 将
request
配置移动到config/web-local.php
文件中:
<?php
return [
'components' => [
'request' => [
'cookieValidationKey' => 'TRk9G1La5kvLFwqMEQTp6PmC1NHdjtkq',
],
],
];
- 从
config/params.php
文件中移除电子邮件ID:
<?php
return [
'adminEmail' => '',
];
- 粘贴ID到
config/params-local.php
文件:
<?php
return [
'adminEmail' => '[email protected]',
];
- 从
tests/codeception/config/config.php
移除dsn
字符串:
<?php
/**
* Application configuration shared by all test types
*/
return [
'controllerMap' => [
// ...
],
'components' => [
'db' => [
'dsn' => '',
],
'mailer' => [
'useFileTransport' => true,
],
'urlManager' => [
'showScriptName' => true,
],
],
];
- 将字符串放到一个新文件中
tests/codeception/config/config-local.php
:
<?php
return [
'components' => [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=yii2_basic_tests',
],
],
];
- 在文件
web/index.php
中添加配置合并:
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../config/common.php'),
require(__DIR__ . '/../config/common-local.php'),
require(__DIR__ . '/../config/web.php'),
require(__DIR__ . '/../config/web-local.php')
);
- 添加配置合并到终端入口脚本,
yii
:
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/config/common.php'),
require(__DIR__ . '/config/common-local.php'),
require(__DIR__ . '/config/console.php'),
require(__DIR__ . '/config/console-local.php')
);
- 添加配置合并到
tests/codeception/config
中含有单元、功能、验收测试的测试配置中:
return yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../../config/common.php'),
require(__DIR__ . '/../../../config/common-local.php'),
require(__DIR__ . '/../../../config/web.php'),
require(__DIR__ . '/../../../config/web-local.php'),
require(__DIR__ . '/config.php'),
require(__DIR__ . '/config-local.php'),
[
// ...
]
);
- 添加配置合并到测试环境控制台的入口脚本,
tests/codeception/bin/yii
:
$config = yii\helpers\ArrayHelper::merge(
require(YII_APP_BASE_PATH . '/config/common.php'),
require(YII_APP_BASE_PATH . '/config/common-local.php'),
require(YII_APP_BASE_PATH . '/config/console.php'),
require(YII_APP_BASE_PATH . '/config/console-local.php'),
require(__DIR__ . '/../config/config.php'),
require(__DIR__ . '/../config/config-local.php')
);
- 结果是,你可以在你的配置文件夹下,获得如下内容:
config
common.php
common-local.php
console.php
console-local.php
web.php
web-local.php
params.php
params-local.php
- 最终,你可以添加一个新的
.gitignore
文件到你的config
和tests/codeception/config
文件夹下,所以你可以通过版本控制系统忽略本地配置文件:
/*-local.php
工作原理...
你可以在config/common.php
文件中存储常见的应用组件配置,同时也可以为web和控制台应用设置指定的配置。你可以将你的临时和安全配置数据放在*-local.php
文件中。
此外,你可以从yii2-app-advanced
中复制初始化shell脚本。
- 创建一个新的
environments
目录,并复制你的模板到里边:
environments
dev
config
common-local.php
console-local.php
web-local.php
params-local.php
web
index.php
index-test.php
tests
codeception
config
config.php
config-local.php
yii
prod
config
common-local.php
console-local.php
web-local.php
params-local.php
web
index.php
yii
- 创建
environments/index.php
文件:
<?php
return [
'Development' => [
'path' => 'dev',
'setWritable' => [
'runtime',
'web/assets',
],
'setExecutable' => [
'yii',
'tests/codeception/bin/yii',
],
'setCookieValidationKey' => [
'config/web-local.php',
],
],
'Production' => [
'path' => 'prod',
'setWritable' => [
'runtime',
'web/assets',
],
'setExecutable' => [
'yii',
],
'setCookieValidationKey' => [
'config/web-local.php',
],
],
];
- 从你的
composer.json
中移除默认的Installer::postCreateProject
配置:
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
- 从高级模板https://github.com/yiisoft/yii2-app-advanced拷贝
init
和init.bat
脚本,你可以使用php init
运行初始化过程,从repository中克隆项目。
参考
欲了解更多关于应用配置的信息,参考http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html。