CodeIgniter 4 - Environment Variables (The Best Way For Application Setup)

Posted by Ridwan Fadilah on Jun 23, 2020 - 09:47 pm

Environment Variables (The Best Way For Application Setup)

Learn how to set your base URL, enable development mode and error reporting, and configure your database using Environment Variables.

CodeIgniter 4 gives users several options to set up their applications. One of these is by using the Environment Variables. As the CodeIgniter Official suggests, the best practice for application set up is to use Environment Variables.

CodeIgniter makes it simple to use Environment Variables by using the 'dot-env' file. In this tutorial, you'll learn how to set your application base URL, activate development mode, enable error reporting, and database configuration.


ENV File

The 'env' file is unusable by default. Before you use the 'env' file, you should set it as a system file first. How to set the 'env' file to be a system file? It's simple, just rename the file by adding the 'dot' in front of the filename.

Rename env

I recommend you to copy it first and use the copy. Keep the original file as the backup file.

Development Mode and Error Report

After you set the 'env' file, now you can start to set up your application using the Environment Variables.

In this file, you'll find many hashtags. The hashtag is a comment tag. Before you use the variables in this file, you should delete the comment tag first. Delete the comment tag only at the line that will use.

env file

Now, to activate development mode, remove the tag at the 'CI_ENVIRONMENT' line, and change the 'production' to 'development.'

Example:

from this:
# CI_ENVIRONMENT = 'production'
to this :
CI_ENVIRONMENT = 'development'
CI_ENVIRONMENT

Now you in the development mode. If you get an error while developing your project, the error report will display like this:

Error Reporting

Set the Base URL

The base URL is can be set in the app.php file. However, as the CodeIgniter Official suggests, it's recommended using the environment to set up this.

In the app section, you'll find the app.baseURL, fill it with your URL.

Example:

app.baseURL = 'http://localhost/ci-4/'
App Base URL

Database Configuration

There's no difference with the examples above to configure your database using Environment Variables.

Scroll down, and you'll find the database section. Delete the tag and fill the field with your database, hostname, username, password, etc.

Example:

database.default.hostname = localhost
database.default.database = ci-4
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi

Important to note, be sure you fill the default database, not the 'tests' database except you in the testing mode.

Database Configuration

That's the CodeIgniter 4 Tutorial about application set up by using Environment Variables. If you want to learn more about CodeIgniter 4, you can see the related article below.

You can also find the full source code of these examples on our GitHub. Thanks for reading this tutorial.


Related Articles