Generally, there are three config files in PhalApi.
$ tree ./PhalAPi/Config
.
├── app.php
├── dbs.php
└── sys.php
The file app.php
contains project configs, dbs.php
includes database configs, and sys.php
kepp system configs for different environments.
We can specify where are these config files in entrance file.
//$ vim ./Public/init.php
// Configurations
DI()->config = new PhalApi_Config_File(API_ROOT . '/Config');
Assume there are some configs as below in file app.php
.
return array(
'version' => '1.1.1',
'email' => array(
'address' => 'chanzonghuang@gmail.com',
);
);
Then we can retrieve configs simply.
// all configs in app.php
DI()->config->get('app'); // array( ... )
// one config in app.php
DI()->config->get('app.version'); // 1.1.1
// multi level config in app.php
DI()->config->get('app.email.address'); // 'chanzonghuang@gmail.com'
We can also add new config files as we want.
Actually, there are serveral enviroments during project development, such as development enviroment, testing enviroment, production enviroment etc. Consequently, we need different config files for different enviroment.
Usually, we maintain different config files with different suffixes. Take production configs as an example. The Config
folder looks like as below after we add new prod files dbs.php.prod
and sys.php.prod
:
$ tree ./PhalApi/Config
.
├── app.php
├── dbs.php
├── dbs.php.prod
├── sys.php
└── sys.php.prod
After that, we can switch to related config files automatically in deployment. Take PHing for example.
We could replace these config files with production config files in build.xml
.
//$ vim ./build.xml
<copy
file="./Config/dbs.php.prod"
tofile="./Config/dbs.php"
overwrite="true" />
<copy
file="./Config/sys.php.prod"
tofile="./Config/sys.php"
overwrite="true" />
Once we excute phing
to deploy, we will see:
[copy] Copying 1 file to /home/phapapi/Config
[copy] Copying 1 file to /home/phapapi/Config
If you have PHP7 installed, you can ignore this part, or you need to install PHP7 first of all.
Please visit laruence/yaconf.
DI()->config
with PhalApi_Config_Yaconf
At entrance file, let's register DI()->config
with PhalApi_Config_Yaconf.
DI()->config = new PhalApi_Config_Yaconf();
Assume we have a test.ini
as below in ./Config
folder.
//$ vim ./Config/test.ini
name="PhalApi"
version="1.3.1"
Then we can retrieve the configs as usual.
var_dump(DI()->config->get('foo')); // same as var_dump(Yaconf::get("foo"));
var_dump(DI()->config->has('foo')); // same as var_dump(Yaconf::has("foo"));
PhalApi save config into files at default. Of course, we can store our configs at anywhere, like database, cache, even remote servers wherever we want. All we need to do is just implementing the interface PhalApi_Config.