Reading Configurations

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

Values can be obtained from Phalcon\Config as follows:

  1. <?php
  2. use Phalcon\Config;
  3. $config = new Config(
  4. [
  5. 'test' => [
  6. 'parent' => [
  7. 'property' => 1,
  8. 'property2' => 'yeah',
  9. ],
  10. ],
  11. ]
  12. );
  13. echo $config->get('test')->get('parent')->get('property'); // displays 1
  14. echo $config->test->parent->property; // displays 1
  15. echo $config->path('test.parent.property'); // displays 1

Factory

Loads Config Adapter class using adapter option, if no extension is provided it will be added to filePath

  1. <?php
  2. use Phalcon\Config\Factory;
  3. $options = [
  4. 'filePath' => 'path/config',
  5. 'adapter' => 'php',
  6. ];
  7. $config = Factory::load($options);

Native Arrays

The first 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 = [
  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";

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);

File Adapters

The adapters available are:

ClassDescription
Phalcon\Config\Adapter\IniUses INI files to store settings. Internally the adapter uses the PHP function 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.

Reading INI Files

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 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(
  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);

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. )

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

Nested Configuration

You may easily access nested configuration values using the Phalcon\Config::path method. This method allows to obtain values, without caring about the fact that some parts of the path are absent. Let’s look at an example:

  1. <?php
  2. use Phalcon\Config;
  3. $config = new Config(
  4. [
  5. 'phalcon' => [
  6. 'baseuri' => '/phalcon/'
  7. ],
  8. 'models' => [
  9. 'metadata' => 'memory'
  10. ],
  11. 'database' => [
  12. 'adapter' => 'mysql',
  13. 'host' => 'localhost',
  14. 'username' => 'user',
  15. 'password' => 'passwd',
  16. 'name' => 'demo'
  17. ],
  18. 'test' => [
  19. 'parent' => [
  20. 'property' => 1,
  21. 'property2' => 'yeah'
  22. ],
  23. ],
  24. ]
  25. );
  26. // Using dot as delimiter
  27. $config->path('test.parent.property2'); // yeah
  28. $config->path('database.host', null, '.'); // localhost
  29. $config->path('test.parent'); // Phalcon\Config
  30. // Using slash as delimiter. A default value may also be specified and
  31. // will be returned if the configuration option does not exist.
  32. $config->path('test/parent/property3', 'no', '/'); // no
  33. Config::setPathDelimiter('/');
  34. $config->path('test/parent/property2'); // yeah

The following example shows how to create usefull facade to access nested configuration values:

  1. <?php
  2. use Phalcon\Di;
  3. use Phalcon\Config;
  4. /**
  5. * @return mixed|Config
  6. */
  7. function config() {
  8. $args = func_get_args();
  9. $config = Di::getDefault()->getShared(__FUNCTION__);
  10. if (empty($args)) {
  11. return $config;
  12. }
  13. return call_user_func_array([$config, 'path'], $args);
  14. }

Injecting Configuration Dependency

You can inject your configuration to the controller allowing us to use Phalcon\Config inside Phalcon\Mvc\Controller. To be able to do that, you have to add it as a service in the Dependency Injector container. Add following code inside your bootstrap file:

  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. }