Posted by Ridwan Fadilah on Jun 23, 2020 - 09:47 pm
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.
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.
I recommend you to copy it first and use the copy. Keep the original file as the backup file.
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.
Now, to activate development mode, remove the tag at the '
CI_ENVIRONMENT' line, and change the 'production' to 'development.'
Now you in the development mode. If you get an error while developing your project, the error report will display like this:
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.
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.
Important to note, be sure you fill the default database, not the 'tests' database except you in the testing mode.
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.