LoginSignup
8
6

More than 5 years have passed since last update.

Drupal 8のローカル開発環境で固有の設定値で上書きする - settings.local.php 設定サンプル

Last updated at Posted at 2018-01-02

Drupal 8の開発を行うとき、大抵の場合はGitリポジトリなどでソースを管理しており、/sites/default/settings.phpもコミットされていることが多いでしょう。

しかし、各開発者はsettings.phpにローカル固有の設定値を持たせたいことがあると思います。

検証環境

  • Macbook Pro (Retina, 15-inch, Mid 2014)
  • Mac OS X 10.11.6 El Capitan
  • PHPStorm 2017.3
  • Vagrant 1.9.4
  • CentOS 7.4.1708
  • Drupal8.4.4

local.settings.phpファイルを使用する

では、プライベートな設定用のファイルを設置して、ローカル固有の設定値を持たせてみましょう。
ファイル名は通常、settings.local.phpという名前でsettings.phpと同じディレクトリに保存します。

ファイル作成手順

1. /sites/defaultディレクトリにsettings.local.phpファイルを作成します。

Screen Shot 2018-01-02 at 15.11.16.png

2. settings.phpの最終行に、以下のインクルード文を追記します。
/**
 * Load local development override configuration, if available.
 */
if (file_exists(__DIR__ . '/sites.local.php')) {
  include __DIR__ . '/sites.local.php';
}
3. Gitを使用している場合は、docroot/sites/*/settings.local.php.gitignoreファイルに追加します。

Screen Shot 2018-01-02 at 15.34.31.png

settings.local.phpファイルの設定値サンプル

settings.local.php ファイルに追加するプライベート設定の主なものに、キャッシュ無効化、ローカル環境固有のディレクトリパスで上書き、エラーをすべて表示などが挙げられます。

例)settings.local.phpのサンプル(このままコピーして保存OK)

<?php

/**
 * @file
 * Local development override configuration feature.
 */

/**
 * Assertions.
 */
assert_options(ASSERT_ACTIVE, TRUE);
\Drupal\Component\Assertion\Handle::register();


/**
 * Enable local development services.
 */
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

/**
 * Show all error messages, with backtrace information.
 */
$config['system.logging']['error_level'] = 'all'; // hide|some|all|verbose

/**
 * Disable CSS and JS aggregation.
 */
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
$config['system.performance']['css']['gzip'] = FALSE;
$config['system.performance']['js']['gzip'] = FALSE;
$config['system.performance']['response']['gzip'] = FALSE;

/**
 * Disable the render cache (this includes the page cache).
 */
$settings['cache']['bins']['render'] = 'cache.backend.null';

/**
 * Disable Dynamic Page Cache.
 */
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

/**
 * Allow test modules and themes to be installed.
 */
$settings['extension_discovery_scan_tests'] = TRUE;

/**
 * Enable access to rebuild.php.
 */
$settings['rebuild_access'] = TRUE;

/**
 * Skip file system permissions hardening.
 */
$settings['skip_permissions_hardening'] = TRUE;


/**
 * Default mode for directories and files written by Drupal.
 */
$settings['file_chmod_directory'] = 0777;
$settings['file_chmod_file'] = 0666;


/**
 * file path settings.
 */
$settings['file_public_path'] = 'sites/default/files';
$settings['file_private_path'] = 'sites/default/private';
$config['system.file']['path']['temporary'] = '/tmp';
$config['system.file']['temporary_maximum_age'] = 86400; // Time in seconds, 0 = never delete


/**
 * SMTP settings.
 */
$config['smtp.settings']['smtp_on'] = true;
$config['smtp.settings']['smtp_host'] = 'mail.example.com';
$config['smtp.settings']['smtp_port'] = '587';
$config['smtp.settings']['smtp_protocol'] = 'standard';
$config['smtp.settings']['smtp_username'] = 'sato@example.com';
$config['smtp.settings']['smtp_password'] = 'password';
$config['system.mail']['interface']['default'] = 'SMTPMailSystem';


/**
 * Views settings.
 */
$config['views.settings']['ui']['show']['sql_query']['enabled'] = TRUE; //Show the SQL query
$config['views.settings']['ui']['show']['additional_queries'] = TRUE; //Show information and statistics about the view during live preview
$config['views.settings']['ui']['show']['performance_statistics'] = TRUE; //Show performance statistics


/**
 * Other.
 */
$config['system.site']['name'] = 'DEVELOPMENT';
$config['system.site']['mail'] = 'admin@gmail.com';
$config['system.performance']['cache']['page']['max_age'] = 0;  // Time in seconds, 0 = no caching
$config['dblog.settings']['row_limit'] = 500;                  // Max. entries in log, 0 = all entries
$config['system.cron']['threshold']['autorun'] = 0;             // Time in seconds, 0 = never run

development.services.ymlファイルの設定値サンプル

settings.local.phpファイルの中でdevelopment.services.ymlをインクルードしていますが、このファイルの中ではTwigキャッシュを無効化しています。

/**
 * Enable local development services.
 */
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

テーマ開発をする際に、テンプレートの編集内容をブラウザに反映するためにDrupalのキャッシュクリアを都度行う必要がありますが、以下の内容でファイル設置しておくとキャッシュ無効化 & Twigデバッグが有効化されます。

例)development.services.ymlのサンプル(このままコピーして保存OK)

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false

services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

サイトに反映するには、ファイル設置した上でDrupalキャッシュクリアを行ってください。
Twigデバッグが有効化されているため、ブラウザからHTMLソースを見るとテンプレートファイルのサジェストが出力されるようになっているはずです。

Screen_Shot_2018-02-24_at_21_54_49.png

ローカル固有の設定値が反映されていることを確認する

settings.local.phpでローカル固有の値で上書きしたものが実際に反映されていることを確認してみましょう。

drush config-getで確認する

確認方法ですが、drush config-getコマンドを利用すると簡単に確認できます。
config-getのエイリアスはcgetです)

データベースに格納されているデフォルト値と、上書きされた値を表示するには以下の通りにコマンド実行します。

デフォルト値を表示する(ローカル固有の上書き値は反映されない)

$ drush cget system.site name
'system.site:name': 'Original site name'

ローカル固有の上書き値が反映されたものを表示する

$ drush cget system.site name --include-overridden
'system.site:name': 'Overridden site name'

このように--include-overriddenを付けると上書き値を表示できます。

drush cgetの詳しい使い方は、drushcommands.comにて確認できます。

Drupal APIで確認する

Drupal APIを使う場合は、以下の通りにコード実装します。

//デフォルト値を表示する(ローカル固有の上書き値は反映されない)
$site_name_original = \Drupal::config('system.site')->getOriginal('name', FALSE);

//ローカル固有の上書き値が反映されたものを表示する
$site_name_overridden = \Drupal::config('system.site')->get('name', FALSE);

まとめ

以上、これらの設定値はあくまで開発環境のためのものなので、settings.phpではなく、settings.local.phpに追記するようにお願いします。

とくにキャッシュ無効化に関する設定値は、開発効率が上がる反面、Drupalのパフォーマンスが低下したりするので、誤ってsettings.phpには追記しないようご注意ください。

8
6
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
8
6