Himanshu SharmaSoftware engineer
Let’s talk

CodeIgniter 4 · Lesson 5 of 30

CodeIgniter configuration: separate environments safely

Configure the local bookshop URL and explain environment overrides without exposing secrets or confusing testing with development.

15 minRequests and presentationUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonConfigure the local bookshop URL and explain environment overrides without exposing secrets or confusing testing with development.

After this lesson you can distinguish application defaults from environment-specific values and configure the local bookshop without borrowing secrets from another project. Configuration tells the same code where it runs and how to reach its resources. It should not contain a hidden second implementation of the catalogue.

Defaults and overrides

CodeIgniter uses configuration classes under app/Config. These provide named, inspectable properties. Environment variables can override supported configuration values when the object is constructed. The official Configuration guide explains this process; do not assume any arbitrary environment name automatically creates a new configuration property.

The starter also includes an env template. A local .env file is one way to provide environment values, but it is not a file to publish or copy from an unrelated application. We do not need any credentials in the course because the database is a local SQLite file. Never print the whole environment to troubleshoot one setting: it may contain secrets unrelated to the setting you wanted.

Here is a nonsecret configuration example for a learner's own disposable application. It is text to place in that application's local environment configuration, not a command that reads an existing environment file.

text
CI_ENVIRONMENT = development
app.baseURL = 'http://127.0.0.1:8097/'

The verified application initially used the production environment because no local environment override was set. This is a useful reminder that a starter's directory name does not determine its runtime mode. Use the documented inspection command rather than guessing.

bash
php spark env

Example output captured on 2026-11; yours will differ. The Spark banner is omitted.

text
Your environment is currently set as production.

Configure the local URL explicitly

For the course's local HTTP verification, app/Config/App.php had these property values. Change the existing properties; do not paste loose property declarations at the top of a PHP file.

php
public string $baseURL = 'http://127.0.0.1:8097/';
public string $indexPage = '';

This PHP fragment was syntax-checked inside the existing App class. The local server was started on that host and port. The empty indexPage assumes requests reach the front controller through the development server's routing. On production hosting, removing index.php from generated URLs also requires appropriate server routing. A configuration value cannot install a rewrite rule for you.

The base URL affects generated links. During verification, an initial mismatch caused a successful form submission to redirect to an unintended address. The fix was to align the local URL configuration with the server we were actually running. A successful database write and a broken redirect can occur in the same request, so diagnose both rather than repeatedly submitting and creating duplicates.

Environments express operational intent

The documented standard environment names include development, production and testing. Testing has special framework behavior for PHPUnit and should not be chosen as an everyday development mode. Production should avoid exposing detailed diagnostics to visitors. Development can show more detail, but only in a suitably restricted workspace.

Changing the environment is not an access-control system. An unprotected write route remains a route even when the environment says production. Similarly, hiding a toolbar does not make uploaded files private. Configuration and application policy solve different problems.

Keep ordinary defaults reviewable in configuration files. Supply deployment-specific secrets through a secure environment mechanism appropriate to the host. A local .env convenience does not justify committing credentials. When configuration caching is later introduced, changes may require invalidating that cache; editing a value alone does not prove the runtime is using it.

Verify one value at a time

To investigate an incorrect link, inspect the generated link and the relevant base URL setting. To investigate a database connection, inspect the configured driver and the expected disposable path. Avoid broad diagnostic dumps. Small checks reduce both noise and the chance of leaking information.

The fetched official Configuration guide and Handling Multiple Environments guide support these distinctions. Our decision to keep the project entirely fictional is a teaching boundary, not a framework requirement.

Common mistakes

A trailing path or wrong port in the base URL can send redirects somewhere unexpected. Treating testing as a stronger form of development can trigger behavior intended for automated tests. Committing a .env file can expose private settings. Finally, changing configuration while ignoring a previously built cache can leave the application using older values. Confirm effective behavior after every relevant configuration change.

Exercise

Explain why a book can be inserted even though the browser arrives at a missing page after submission. Identify which setting you would inspect first, then describe a safe way to confirm the current environment without dumping all process variables.

Show an answer

The insert happens before the redirect. If the redirect target is generated with an incorrect base URL, the next request can fail despite a successful write. Inspect baseURL and indexPage alongside the actual Location response. Use php spark env to inspect the environment name instead of printing all environment variables.

Previous lesson · Next lesson · Related: security boundaries

Key points

  • Configuration classes define application defaults.
  • Environment selection does not replace authorization.
  • Check generated redirect targets after changing the base URL.

Check yourself

1. Why can an insert succeed before a redirect fails?
2. Which environment is reserved for PHPUnit behavior?
3. What is a safer configuration diagnostic?