读取配置Reading Configurations

Phalcon\Config 是一个用于将各种格式的配置文件读取到PHP对象的组件(使用适配器)。

Phalcon\Config is a component used to read configuration files of various formats (using adapters) into PHP objects for use in an application.

文件适配器File Adapters

可用的适配器有:

The adapters available are:

File TypeDescription
IniUses INI files to store settings. Internally the adapter uses the PHP function parse_ini_file.
ArrayUses PHP multidimensional arrays to store settings. This adapter offers the best performance.

原生数组Native Arrays

下面的例子展示如何将本地数组导入 PhalconConfig 对象。此选项提供了最好的性能,因为在这个请求中没有读取文件。

The next example shows how to convert native arrays into Phalcon\Config objects. This option offers the best performance since no files are read during this request.

  1. <?php
  2. use Phalcon\Config;
  3. $settings = array(
  4. "database" => array(
  5. "adapter" => "Mysql",
  6. "host" => "localhost",
  7. "username" => "scott",
  8. "password" => "cheetah",
  9. "dbname" => "test_db",
  10. ),
  11. "app" => array(
  12. "controllersDir" => "../app/controllers/",
  13. "modelsDir" => "../app/models/",
  14. "viewsDir" => "../app/views/",
  15. ),
  16. "mysetting" => "the-value"
  17. );
  18. $config = new Config($settings);
  19. echo $config->app->controllersDir, "\n";
  20. echo $config->database->username, "\n";
  21. echo $config->mysetting, "\n";

如果你想更好的组织你的项目,你可以在另一个文件保存数组,然后读入它。

If you want to better organize your project you can save the array in another file and then read it.

  1. <?php
  2. use Phalcon\Config;
  3. require "config/config.php";
  4. $config = new Config($settings);

读取 INI 文件Reading INI Files

INI文件是存储设置的常用方法。Phalcon\Config 采用优化的PHP函数parse_ini_file读取这些文件。为方便访问,文件部分解析成子设置。

Ini files are a common way to store settings. Phalcon\Config uses the optimized PHP function parse_ini_file to read these files. Files sections are parsed into sub-settings for easy access.

  1. [database]
  2. adapter = Mysql
  3. host = localhost
  4. username = scott
  5. password = cheetah
  6. dbname = test_db
  7. [phalcon]
  8. controllersDir = "../app/controllers/"
  9. modelsDir = "../app/models/"
  10. viewsDir = "../app/views/"
  11. [models]
  12. metadata.adapter = "Memory"

你可以阅读如下所示的文件:

You can read the file as follows:

  1. <?php
  2. use Phalcon\Config\Adapter\Ini as ConfigIni;
  3. $config = new ConfigIni("path/config.ini");
  4. echo $config->phalcon->controllersDir, "\n";
  5. echo $config->database->username, "\n";
  6. echo $config->models->metadata->adapter, "\n";

合并配置Merging Configurations

Phalcon\Config 允许合并配置对象到另一个:

Phalcon\Config can recursively merge the properties of one configuration object into another. New properties are added and existing properties are updated.

  1. <?php
  2. use Phalcon\Config;
  3. $config = new Config(array(
  4. 'database' => array(
  5. 'host' => 'localhost',
  6. 'dbname' => 'test_db'
  7. ),
  8. 'debug' => 1,
  9. ));
  10. $config2 = new Config(array(
  11. 'database' => array(
  12. 'dbname' => 'production_db',
  13. 'username' => 'scott',
  14. 'password' => 'secret',
  15. ),
  16. 'logging' => 1,
  17. ));
  18. $config->merge($config2);
  19. print_r($config);

上面的代码会产生以下内容:

The above code produces the following:

  1. Phalcon\Config Object
  2. (
  3. [database] => Phalcon\Config Object
  4. (
  5. [host] => localhost
  6. [dbname] => production_db
  7. [username] => scott
  8. [password] => secret
  9. )
  10. [debug] => 1
  11. [logging] => 1
  12. )

有更多的适配器可用于这个组件: Phalcon Incubator

There are more adapters available for this components in the Phalcon Incubator