Environment variables

It is often helpful to have different configuration values based on the environment where the application is running.

For example, you may wish to use a different cache driver locally than you do on your production server.

Kiaan's default resources\settings.env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Kiaan configuration files within the config directory using Kiaan's env function.

Define a variable

To define a variable, it must be defined on a single line

resources\settings.env
APP_NAME=MyApplication

Comments

You can define the line as a comment if preceded by "#"

resources\settings.env
# App_title=App

Assign the value

To add a value to an existing variable or create it if it does not exist and assign the value to it.

Env::set("name", "Hassan");

and You can change key name of environment variable.

Env::name("name", "my_name");

Check variables

To check if a variable exists, it returns true, and if it doesn't, it returns false.

Env::has("name");

Get value

Get value of variables.

Env::get("name");
// Or
getenv("name");
// Or
$__ENV("name");

use env() helper function to get value of variables.

env("name");

and you can get all variables using all() method.

Env::all();
// Or
$__ENV

Delete variables

You can delete the environment variable using delete() method.

Env::delete("name");

You can delete all the environment variable use destroy() method.

Env::destroy();

Load

You can also load content with a string.

Env::load()->content("
            App = Application
            Version = 1.0
        ")->get("Version");

and load content from file .env.

Env::load()->file(".env")->get("Version")

Last updated