View on GitHub

yii2-cookbook-chinese

Yii Application Development Cookbook(Third Edition)中文翻译

使用多个配置来简化部署

高级应用模板为它的每一个应用使用不同的配置文件。

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应用。

如何做…

  1. 创建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,
];
  1. 创建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,
        ],
    ],
];
  1. 移除config/db.php文件:
  2. 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'],
                ],
            ],
        ],
    ],
];
  1. 创建一个带有空数组的文件config/console-local.php
<?php return [];
  1. 修改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;
  1. request配置移动到config/web-local.php文件中:
<?php
return [
    'components' => [
        'request' => [
            'cookieValidationKey' => 'TRk9G1La5kvLFwqMEQTp6PmC1NHdjtkq',
        ],
    ],
];
  1. config/params.php文件中移除电子邮件ID:
<?php
return [
    'adminEmail' => '',
];
  1. 粘贴ID到config/params-local.php文件:
<?php
return [
    'adminEmail' => 'admin@example.com',
];
  1. 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,
        ],
    ],
];
  1. 将字符串放到一个新文件中tests/codeception/config/config-local.php
<?php
return [
    'components' => [
        'db' => [
            'dsn' => 'mysql:host=localhost;dbname=yii2_basic_tests',
        ],
    ],
];
  1. 在文件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')
);
  1. 添加配置合并到终端入口脚本,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')
);
  1. 添加配置合并到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'),
    [
        // ...
    ]
);
  1. 添加配置合并到测试环境控制台的入口脚本,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')
);
  1. 结果是,你可以在你的配置文件夹下,获得如下内容:
config
    common.php
    common-local.php
    console.php
    console-local.php
    web.php
    web-local.php
    params.php
    params-local.php
  1. 最终,你可以添加一个新的.gitignore文件到你的configtests/codeception/config文件夹下,所以你可以通过版本控制系统忽略本地配置文件:
/*-local.php

工作原理…

你可以在config/common.php文件中存储常见的应用组件配置,同时也可以为web和控制台应用设置指定的配置。你可以将你的临时和安全配置数据放在*-local.php文件中。

此外,你可以从yii2-app-advanced中复制初始化shell脚本。

  1. 创建一个新的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
  1. 创建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',
        ],
    ],
];
  1. 从你的composer.json中移除默认的Installer::postCreateProject配置:
"extra": {
    "asset-installer-paths": {
        "npm-asset-library": "vendor/npm",
        "bower-asset-library": "vendor/bower"
    }
}
  1. 从高级模板https://github.com/yiisoft/yii2-app-advanced拷贝initinit.bat脚本,你可以使用php init运行初始化过程,从repository中克隆项目。

参考

欲了解更多关于应用配置的信息,参考http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html