Configuration

Accessing the application's configuration and environment variables

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.

Environment Variables

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:

mixed env( string $variable_name [, mixed $default_value = null ])

Parameter

Type

Required

Default

Description

$variable_name

string

Yes

none

The name of the environment variable to retrieve

$default_value

mixed

No

null

The value to return if the variable is not found/declared

Returns 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 true.

  • 'false' or '(false)' will be converted to PHP's boolean false.

  • 'empty' or '(empty)' will be converted to an empty string ''.

  • 'null' or '(null)' will be converted to void/undefined.

  • values enclosed in double quotes will have their quotes stripped e.g. "I have spaces" will be stripped to I have spaces.

Configuration Files

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:

mixed config( string$key [, mixed $value = null ])

Parameter

Type

Required

Default

Description

$key

string

Yes

none

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.

$value

mixed

No

null

The value to set on the config variable during run-time.

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