读取配置(Reading Configurations)

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

原生数组(Native Arrays)

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

  1. <?php
  2. use Phalcon\Config;
  3. $settings = [
  4. "database" => [
  5. "adapter" => "Mysql",
  6. "host" => "localhost",
  7. "username" => "scott",
  8. "password" => "cheetah",
  9. "dbname" => "test_db"
  10. ],
  11. "app" => [
  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";

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

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

文件适配器(File Adapters)

可用的适配器有:

Class解释
Phalcon\Config\Adapter\Ini使用INI文件来存储设置。内部适配器使用PHP函数 parse_ini_file。
Phalcon\Config\Adapter\JsonUses JSON files to store settings.
Phalcon\Config\Adapter\PhpUses PHP multidimensional arrays to store settings. This adapter offers the best performance.
Phalcon\Config\Adapter\YamlUses YAML files to store settings.

读取 INI 文件(Reading INI Files)

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

  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"

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

  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 允许合并配置对象到另一个:

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

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

  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

Injecting Configuration Dependency

You can inject configuration dependency to controller allowing us to use Phalcon\Config inside Phalcon\Mvc\Controller. To be able to do that, add following code inside your dependency injector script.

  1. <?php
  2. use Phalcon\Di\FactoryDefault;
  3. use Phalcon\Config;
  4. // Create a DI
  5. $di = new FactoryDefault();
  6. $di->set(
  7. "config",
  8. function () {
  9. $configData = require "config/config.php";
  10. return new Config($configData);
  11. }
  12. );

Now in your controller you can access your configuration by using dependency injection feature using name config like following code:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class MyController extends Controller
  4. {
  5. private function getDatabaseName()
  6. {
  7. return $this->config->database->dbname;
  8. }
  9. }