CakePHP 4 How To Use Application Environment Variables

Reading Time: 4 minutes
3,877 Views

Inside this article we will see CakePHP 4 how to use application environment variables. Environment variables are just like constant variables in any application which can be used through out the application.

Environment variables will be defined inside .env file. The value of file will not be changed through out the application life. By default in CakePHP 4 you will get .env.example file. To use environment variables we need to rename it to .env

Inside this tutorial we will see complete idea to How to use even How to create environment variables in CakePHP 4. Article contains classified information about the Environment variables in CakePHP 4.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

File: .env.example

When we download the CakePHP 4 setup by default you will get .env.example inside /config folder. Inside this file you will see several lines of codes added. Few are comments and few are activated.

//...

export APP_NAME="__APP_NAME__"
export DEBUG="true"
export APP_ENCODING="UTF-8"
export APP_DEFAULT_LOCALE="en_US"
export APP_DEFAULT_TIMEZONE="UTC"
export SECURITY_SALT="__SALT__"

//...

To use this environment variables in application we need to do :

  • Rename or copy .env.example to .env
  • Uncomment lines from bootstrap.php file

The simple way to rename this file.

But this is not over yet. Still we will not be able to use the environment variables. Next, we need to uncomment few lines from bootstrap.php file from /config folder.

Search these lines and ucomment that.

//...

if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
    $dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
    $dotenv->parse()
        ->putenv()
        ->toEnv()
        ->toServer();
}

//...

Now, we have done everything to get access over .env.

Additionally, we can add custom environment variables into this file.

Get Environment Values

To access environment variables values, CakePHP application provides few helper functions. We use those functions and access the value of .env variables.

Function #1 – env()

Here, we have env() function. You need to pass the key name.

echo env("APP_NAME");

Function #2 – getenv()

Here, we have getenv() function. You need to pass the key name.

echo getenv("APP_NAME");

Custom Environment Variable

To define custom environment variables means custom constants into .env file.

Open .env file and add like that –

MYVAR="This is custom variable value"

OR 

export MYVAR="This is custom variable value"

Next,

To access value we use –

echo env("MYVAR");

We hope this article helped you to learn about CakePHP 4 How To Use Application Environment Variables Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.