メインコンテンツまでスキップ
Version: Fegg 5.x

Application::loadConfig

設定ファイルを読み込みます

説明​

public array Application::loadConfig( string $name, string $languageCode = '' )

設定ファイル{$name}.phpを読み込み、その変数を$this->config[$name]に入れて返します。

設定ファイルでは、ファイル名と同じ名前の変数を定義します(sample.phpなら$sample)。ファイルは次の順に探し、最初に見つかったファイルを読み込みます。

  1. htdocs/_fegg/site/config/{$name}.php
  2. htdocs/_fegg/core/config/{$name}.php

そのため、サイトに同じ名前のファイルを置くと、コアのファイル(たとえばvalidation_error_message.php)の代わりに使われます。設定でglobal_config_dirを指定している場合は、そのディレクトリのファイルを先に読み込み、上記のファイルでその値を上書きできます。

多言語サイト(default_languageとsupport_languageを設定している場合)では、その後にconfig/{言語コード}/{$name}.phpを読み込み、その言語の値を上書きできます。

結果はキャッシュします。同じ名前でloadConfig()をもう一度呼び出すと、ファイルを読まずに$this->config[$name]を返します。変数を定義したファイルが見つからない場合は、Can't Read Config File: {$name}と出力して止まります。

引数​

名前データ型デフォルト備考
$namestring.phpを除いた設定ファイル名(ファイルで定義する変数の名前でもあります)
$languageCodestring''言語ごとのファイルの言語コード。省略すると現在の言語

例​

htdocs/_fegg/site/config/sample.php
<?php
defined('FEGG_CORE_DIR') || exit;

// The variable name must be the same as the file name
$sample = array();

$sample['foo'] = 'bar';
$sample['baz'] = 'qux';
Controller
$this->loadConfig('sample');

var_dump($this->config['sample']);
Output
array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(3) "qux" }

戻り値をそのまま使うこともできます。

Controller
$sample = $this->loadConfig('sample');
echo $sample['foo']; // -> bar

site_info.phpとcategory_info.phpは、ベースのコントローラーの__init()が読み込みます。そのため、parent::__init()を呼び出すすべてのコントローラーで、$this->config['site_info']と$this->config['category_info']を使えます。