Skip to content

Phalcon config

NOTE

All classes are prefixed with Phalcon

Config\Adapter\Grouped

Class Source on GitHub

Reads multiple files (or arrays) and merges them all together.

See Phalcon\Config\ConfigFactory::load To load Config Adapter class using 'adapter' option.

use Phalcon\Config\Adapter\Grouped;

$config = new Grouped(
    [
        "path/to/config.php",
        "path/to/config.dist.php",
    ]
);
use Phalcon\Config\Adapter\Grouped;

$config = new Grouped(
    [
        "path/to/config.json",
        "path/to/config.dist.json",
    ],
    "json"
);
use Phalcon\Config\Adapter\Grouped;

$config = new Grouped(
    [
        [
            "filePath" => "path/to/config.php",
            "adapter"  => "php",
        ],
        [
            "filePath" => "path/to/config.json",
            "adapter"  => "json",
        ],
        [
            "adapter"  => "array",
            "config"   => [
                "property" => "value",
            ],
        ],
    ],
);

Uses Phalcon\Config\Config · Phalcon\Config\ConfigFactory · Phalcon\Config\ConfigInterface · Phalcon\Config\Exception · Phalcon\Config\Exceptions\GroupedAdapterRequiresArray · Phalcon\Factory\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    array $arrayConfig,
    string $defaultAdapter = "php"
);

Phalcon\Config\Adapter\Grouped constructor

Config\Adapter\Ini

Class Source on GitHub

Reads ini files and converts them to Phalcon\Config\Config objects.

Given the next configuration file:

[database]
adapter = Mysql
host = localhost
username = scott
password = cheetah
dbname = test_db

[phalcon]
controllersDir = "../app/controllers/"
modelsDir = "../app/models/"
viewsDir = "../app/views/"

You can read it as follows:

use Phalcon\Config\Adapter\Ini;

$config = new Ini("path/config.ini");

echo $config->phalcon->controllersDir;
echo $config->database->username;

PHP constants may also be parsed in the ini file, so if you define a constant as an ini value before calling the constructor, the constant's value will be integrated into the results. To use it this way you must specify the optional second parameter as INI_SCANNER_NORMAL when calling the constructor:

$config = new \Phalcon\Config\Adapter\Ini(
    "path/config-with-constants.ini",
    INI_SCANNER_NORMAL
);

Uses Phalcon\Config\Config · Phalcon\Config\Exception · Phalcon\Config\Exceptions\CannotLoadConfigFile

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $filePath,
    int $mode = 1
);

Ini constructor.

Protected · 4

cast()

protected function cast( mixed $ini ): mixed;

We have to cast values manually because parse_ini_file() has a poor implementation.

castArray()

protected function castArray( array $ini ): array;

parseIniString()

protected function parseIniString(
    string $path,
    mixed $value
): array;

Build multidimensional array from string

phpParseIniFile()

protected function phpParseIniFile(
    string $filename,
    bool $processSections = false,
    int $scannerMode = 1
);

@todo to be removed when we get traits

Config\Adapter\Json

Class Source on GitHub

Reads JSON files and converts them to Phalcon\Config\Config objects.

Given the following configuration file:

{"phalcon":{"baseuri":"\/phalcon\/"},"models":{"metadata":"memory"}}

You can read it as follows:

use Phalcon\Config\Adapter\Json;

$config = new Json("path/config.json");

echo $config->phalcon->baseuri;
echo $config->models->metadata;

Uses Phalcon\Config\Config · Phalcon\Support\Helper\Json\Decode

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $filePath );

Phalcon\Config\Adapter\Json constructor

Config\Adapter\Php

Class Source on GitHub

Reads php files and converts them to Phalcon\Config\Config objects.

Given the next configuration file:

<?php

return [
    "database" => [
        "adapter"  => "Mysql",
        "host"     => "localhost",
        "username" => "scott",
        "password" => "cheetah",
        "dbname"   => "test_db",
    ],
    "phalcon" => [
        "controllersDir" => "../app/controllers/",
        "modelsDir"      => "../app/models/",
        "viewsDir"       => "../app/views/",
    ],
];

You can read it as follows:

use Phalcon\Config\Adapter\Php;

$config = new Php("path/config.php");

echo $config->phalcon->controllersDir;
echo $config->database->username;

Uses Phalcon\Config\Config

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $filePath );

Phalcon\Config\Adapter\Php constructor

Config\Adapter\Yaml

Class Source on GitHub

Reads YAML files and converts them to Phalcon\Config\Config objects.

Given the following configuration file:

phalcon:
  baseuri:        /phalcon/
  controllersDir: !approot  /app/controllers/
models:
  metadata: memory

You can read it as follows:

define(
    "APPROOT",
    dirname(__DIR__)
);

use Phalcon\Config\Adapter\Yaml;

$config = new Yaml(
    "path/config.yaml",
    [
        "!approot" => function($value) {
            return APPROOT . $value;
        },
    ]
);

echo $config->phalcon->controllersDir;
echo $config->phalcon->baseuri;
echo $config->models->metadata;

Uses Phalcon\Config\Config · Phalcon\Config\Exception · Phalcon\Config\Exceptions\CannotLoadConfigFile · Phalcon\Config\Exceptions\MissingYamlExtension

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $filePath,
    array $callbacks = null
);

Phalcon\Config\Adapter\Yaml constructor

Protected · 2

phpExtensionLoaded()

protected function phpExtensionLoaded( string $name ): bool;

phpYamlParseFile()

protected function phpYamlParseFile(
    mixed $filename,
    mixed $pos = 0,
    mixed $ndocs = null,
    mixed $callbacks = []
);

@todo to be removed when we get traits

Config\Config

Class Source on GitHub

Phalcon\Config is designed to simplify the access to, and the use of, configuration data within applications. It provides a nested object property based user interface for accessing this configuration data within application code.

$config = new \Phalcon\Config\Config(
    [
        "database" => [
            "adapter"  => "Mysql",
            "host"     => "localhost",
            "username" => "scott",
            "password" => "cheetah",
            "dbname"   => "test_db",
        ],
        "phalcon" => [
            "controllersDir" => "../app/controllers/",
            "modelsDir"      => "../app/models/",
            "viewsDir"       => "../app/views/",
        ],
    ]
);

Uses Phalcon\Config\Exceptions\InvalidMergeData · Phalcon\Support\Collection

Method Summary

Constants

string DEFAULT_PATH_DELIMITER = "."

Properties

protected string $pathDelimiter = self::DEFAULT_PATH_DELIMITER

Methods

Public · 5

getPathDelimiter()

public function getPathDelimiter(): string;

Gets the default path delimiter

merge()

public function merge( mixed $toMerge ): ConfigInterface;

Merges a configuration into the current one

$appConfig = new \Phalcon\Config\Config(
    [
        "database" => [
            "host" => "localhost",
        ],
    ]
);

$globalConfig->merge($appConfig);

path()

public function path(
    string $path,
    mixed $defaultValue = null,
    string $delimiter = null
): mixed;

Returns a value from current config using a dot separated path.

echo $config->path("unknown.path", "default", ".");

setPathDelimiter()

public function setPathDelimiter( string $delimiter = null ): ConfigInterface;

Sets the default path delimiter

toArray()

public function toArray(): array;

Converts recursively the object to an array

print_r(
    $config->toArray()
);
Protected · 2

internalMerge()

final protected function internalMerge(
    array $source,
    array $target
): array;

Performs a merge recursively

setData()

protected function setData(
    mixed $element,
    mixed $value
): void;

Sets the collection data

Config\ConfigFactory

Class Source on GitHub

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

use Phalcon\Config\ConfigFactory;

$options = [
    "filePath" => "path/config",
    "adapter"  => "php",
];

$config = (new ConfigFactory())->load($options);

Uses Phalcon\Config\Config · Phalcon\Config\ConfigInterface · Phalcon\Config\Exceptions\ConfigNotArrayOrObject · Phalcon\Config\Exceptions\MissingConfigOption · Phalcon\Config\Exceptions\MissingFileExtension · Phalcon\Factory\AbstractFactory

Method Summary

Methods

Public · 3

__construct()

public function __construct( array $services = [] );

ConfigFactory constructor.

load()

public function load( mixed $config ): ConfigInterface;

Load a config to create a new instance

newInstance()

public function newInstance(
    string $name,
    string $fileName,
    mixed $params = null
): ConfigInterface;

Returns a new Config instance

Protected · 3

getExceptionClass()

protected function getExceptionClass(): string;

getServices()

protected function getServices(): array;

Returns the available adapters

parseConfig()

protected function parseConfig( mixed $config ): array;

Config\ConfigInterface

Interface Source on GitHub

Phalcon\Config\ConfigInterface

Interface for Phalcon\Config\Config class

Uses Phalcon\Support\Collection\CollectionInterface

Method Summary

Methods

Public · 4

getPathDelimiter()

public function getPathDelimiter(): string;

merge()

public function merge( mixed $toMerge ): ConfigInterface;

path()

public function path(
    string $path,
    mixed $defaultValue = null,
    string $delimiter = null
): mixed;

setPathDelimiter()

public function setPathDelimiter( string $delimiter = null ): ConfigInterface;

Config\Exception

Class Source on GitHub

Exceptions thrown in Phalcon\Config will use this class

Config\Exceptions\CannotLoadConfigFile

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 2

__construct()

public function __construct( string $fileName );

getFileName()

public function getFileName(): string;

Config\Exceptions\ConfigNotArrayOrObject

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\GroupedAdapterRequiresArray

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\InvalidMergeData

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\MissingConfigOption

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 2

__construct()

public function __construct( string $option );

getOption()

public function getOption(): string;

Config\Exceptions\MissingFileExtension

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Config\Exceptions\MissingYamlExtension

Class Source on GitHub

This file is part of the Phalcon Framework.

(c) Phalcon Team team@phalcon.io

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Uses Phalcon\Config\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();