LoginSignup
3
2

More than 1 year has passed since last update.

WinterCMS (OctoberCMS) 環境別の設定

Last updated at Posted at 2016-09-11

Webアプリの開発を行っていると、ローカルマシン、テストサーバ、本番環境などと実行環境を幾つか使い分けると思います。

WinterCMSは環境別の設定を促すものはありませんが、Laravelと同様に.envで環境別に設定を切り替える事ができます。

Laravel (つまりOctoberCMS)では、.envファイルに設定を記述して、env()で読み込んでconfig/app.phpなどの設定ファイルに動的に値を設定します。
.envはコミットせず、ソースとは別管理してデプロイ時に持ってくるようにします。DBパスワードなどが記述されているケースが多いので、特定の人のみがアクセスできるファイルサーバ(S3など)に保管し、デプロイスクリプトで持ってくるようにするのをお勧めします。(ただ、各エンジニアの開発環境を作成する際に.envファイルを作成しやすいように、サンプル.envファイルをソースツリーにコミットしておくと親切ですね。)

これはOctoberCMSの公式ドキュメントでも軽く説明されていますね。

以下は、config/app.phpconfig/database.phpを自分のローカル環境用に設定する例です。

.env
# pkanji's .evn
# Do NOT commit this file.

#################################################
# For config/app.php
#################################################
APP_ENV=local
APP_DEBUG=true
APP_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_URL=http://localhost
APP_TIMEZONE=Asia/Tokyo
APP_LOG=single

#################################################
# For config/database.php
#################################################
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=my_database
DB_PASSWORD=khk6Agq1keJ_ke

config/app.phpconfig/database.phpの方へは、env()でそれぞれ設定します。Laravel5だとここらへんははじめから設定されていたと思います。

config/app.php
<?php
return [
    'debug' => env('APP_DEBUG', false),
    'url' => env('APP_URL', 'http://localhost'),
    'locale' => 'ja',
    'fallback_locale' => 'en',
    'key' => env('APP_KEY'),
    'cipher' => 'AES-256-CBC',
    'log' => env('APP_LOG', 'single'),
    ...
];
config/database.php
<?php
return [
    'fetch' => PDO::FETCH_CLASS,
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_general_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
    ...
];

簡単ですね。.envに設定値を切り離して、config/*.phpで適宜env()で設定するだけです。

注意

なお、env()config/*.php以外からは使用すべきではないです。なぜなら、.envファイルとconfig/*.phpはコンパイルしてキャッシュされ、キャッシュされた後はenv()nullを返します。

Laravel公式ドキュメントより;

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

collationについて

話が逸れますが、collationでデフォルトcollationが設定できるのは嬉しいですね。これを設定しておかないとcollationのデフォルトはutf8_unicode_ciになります。
utf8_unicode_ciだと、データベース検索で「ハ」と「パ」が区別されないなどの問題が発生します。逆に、utf8_general_ciにすることによる弊害は知る限りありません。(参考
ということで、わざわざutf8_general_ciに設定しているのでした。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2