All config variables can be set in appropriate
app/config file. By default
app/config directory contains below three files:
If you go throgh the code of each of these file you will notice that each one returns an associative array. This is how config variables are defined in PHPasap. All the keys and values of the associative array are our config variables and their corresponding values.
Config::get('file.variable')where file is the name of the config file, followed by a dot and then a variable which is nothing but an array key defined in file.
For example to get the config value for debug option in
app.php we would use the below syntax
$debug = Config::get("app.debug"); //OR get with a default value $debug = Config::get("app.debug", true); //if debug not found then default true will be returned
Second param is optional. It is used as default value if the item queried for doesn't exist.
If the config item or config file doesn't exist and no default value is set then
null is returned.
You can add your own config items in any of the config file. So suppose you added below line to the
"api_key" => "anyrandomstring"
Now to access our new config item you would use the following syntax
You can even add your own config file. Like say go ahead and create a
mail.php file in config directory. Add the below code to it.
return [ "smtp_user" => "abc", "smtp_password" => "xyz", "smtp_host" => "lmn", ];
Then you could access any of the config item in your newly created config file using below syntax:
echo Config::get("mail.smtp_user"); //abc echo Config::get("mail.smtp_password"); //xyz echo Config::get("mail.smtp_host"); //lmn
When Config::get() is called without any param then all the config items are returned. The returned value is associative array of associative array. Each file name is array key having value which itself is an array of config items in the corresponding config file.
$config = Config::get();
Doing a var_dump on
$config in above code will give something as below
Array ( [alias] => Array ( ) [app] => Array ( [debug] => 1 ) [database] => Array ( [hostname] => [database] => [username] => [password] => ) )