From time to time, one needs to access variables set in the environment or configuration files. We will explain how this is done in Simcify here.
At the root of Simcify's directory structure, there is a file named
.env.example. This file describes all environment variables that should are for Simcify to work correctly. A copy of this file should be made in the same location and named
.env. The .env file should be edited and correct values set for each required variable. Custom variables you may require in your application can also be defined here.
To retrieve environment variables, Simcify provides a helper function
env. Usage is described below:
$default_value = null
The name of the environment variable to retrieve
The value to return if the variable is not found/declared
mixed - The value of the variable requested or the default value otherwise.
env()will convert string values like shown below:
'true' or '(true)' will be converted to PHP's boolean
'false' or '(false)' will be converted to PHP's boolean
'empty' or '(empty)' will be converted to an empty string
'null' or '(null)' will be converted to
values enclosed in double quotes will have their quotes stripped e.g. "I have spaces" will be stripped to
I have spaces.
At the root of Simcify's directory structure there is a directory named
config. This folder stores config values categorised by file name e.g. All authentication configuration variables will be stored in auth.php.
Configurations stored in this files may also be extracted from the environment variables and it is recommended that all environment variables have a corresponding config variable. This is because unlike environment variables which are declared only once and are not mutable, config variables are mutable and can be changed during the run-time of a request.
To retrieve config variables or set them during run-time, Simcify provides the helper function
config which acts as a getter/setter polymorphic function. Usage is described below:
$value = null
The key of the configuration variable to get in dot syntax i.e. database.driver which will access the value 'driver' from the 'database.php' file.
The value to set on the config variable during run-time.
mixed - Returns the value of the config variable whose $key is provided when $value is null or not provided or sets the config variable to the value provided otherwise.
As you can see here, config will act as a getter when used with one parameter and as a setter method when two parameters are passed.