Enable developer mode in CodeIgniter 4

- Andrés Cruz

En español

The .env file is a common configuration file used in many web frameworks and CodeIgniter 4 is no exception; this file is used to store environment variables that need to be accessed in the application, such as the database, access to APIs that we have created, among others; We can create several types of .env depending on the environment and it is particularly useful in work teams in which each member of the team needs to have different keys.

Let's start with the most important point in this chapter, or at least the most basic, that of being able to see the errors that are going to happen when we develop the application. Before this, to understand the importance of enabling this mode, we are going to cause a syntax error in our application to see what happens; in the File:


What we have:

namespace App\Controllers;
class Home extends BaseController
    public function index()
        return view('welcome_message');

Let's remove a ";" for the view('welcome_message') and when going to our app:

You will see that it tells us that an error occurred, but not where: to see what is happening, we would have to consult a log file that is in:


See the last log and detail the first lines to know what is happening:

CRITICAL - 2022-01-15 15:07:15 --> syntax error, unexpected '}', expecting ';'

In this case, the error that we intentionally cause; obviously, consulting this file every time we have an error is a bit of a headache, ideally the error should appear on the screen when we are in development mode.

Seeing the errors in the log file is ideal for managing the app when we are in a production environment, since the details of the errors are left to us, the developers, and with this we avoid giving details about the structure of our application to a user who could take advantage of the bug detail to exploit a vulnerability in the application.

To enable developer mode, just rename the file called "env" found in the root of our project to ".env", that is, add a point in front:


Enter said file, and uncomment:

# CI_ENVIRONMENT = production


CI_ENVIRONMENT = development

And remember to change "production" to "development"

Now with this, if we go to the page with the error, we will see the detail on the screen:

.env files are used to create project environment variables; standard configurations such as the database, base url, among others of the project and customized by you (we can create custom variables that do not exist in the project, for example, for PayPal, Stripe, etc. credentials).

Although, you must take into account that standard configurations like the ones mentioned above, which have their equivalent in configuration files, when using the production application, must be removed from the .env to use the corresponding configuration file; in case it is necessary for your project, the .env file can also exist in production environment (or other environments that you determine for your project).

In the end, environment variables are dynamic variables that affect the behavior of the system and can exist in different environments.

Remember to revert the bug we intentionally placed.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy