Skip to content

Commit a5caa4d

Browse files
totoprayogo1916michalsnpaulbalandanddevsr
authored
feat: customizable .env directory path (#9631)
* feat: add environment directory to load .env file * fix: rename environment directory variable from environmentDirectory to envDirectory Co-authored-by: Michal Sniatala <michal@sniatala.pl> * docs: add information about changing .env file location Co-authored-by: Michal Sniatala <michal@sniatala.pl> * fix: improve environment variable loading with fallback to ROOTPATH * fix: dynamic path for .env file Co-authored-by: Michal Sniatala <michal@sniatala.pl> * fix: improve wording for .env file location recommendation Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> Co-authored-by: Michal Sniatala <michal@sniatala.pl> Co-authored-by: ddevsr <97607754+ddevsr@users.noreply.github.com> * docs(chngelog): add support for changing the location of the .env file via Paths::$envDirectory property --------- Co-authored-by: Michal Sniatala <michal@sniatala.pl> Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> Co-authored-by: ddevsr <97607754+ddevsr@users.noreply.github.com>
1 parent d1ee696 commit a5caa4d

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

app/Config/Paths.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,16 @@ class Paths
7575
* is used when no value is provided to `Services::renderer()`.
7676
*/
7777
public string $viewDirectory = __DIR__ . '/../Views';
78+
79+
/**
80+
* ---------------------------------------------------------------
81+
* ENVIRONMENT DIRECTORY NAME
82+
* ---------------------------------------------------------------
83+
*
84+
* This variable must contain the name of the directory where
85+
* the .env file is located.
86+
* Please consider security implications when changing this
87+
* value - the directory should not be publicly accessible.
88+
*/
89+
public string $envDirectory = __DIR__ . '/../../';
7890
}

system/Boot.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ public static function preload(Paths $paths): void
170170
protected static function loadDotEnv(Paths $paths): void
171171
{
172172
require_once $paths->systemDirectory . '/Config/DotEnv.php';
173-
(new DotEnv($paths->appDirectory . '/../'))->load();
173+
$envDirectory = $paths->envDirectory ?? $paths->appDirectory . '/../';
174+
(new DotEnv($envDirectory))->load();
174175
}
175176

176177
protected static function defineEnvironment(): void

system/Commands/Encryption/GenerateKey.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Config\DotEnv;
1919
use CodeIgniter\Encryption\Encryption;
20+
use Config\Paths;
2021

2122
/**
2223
* Generates a new encryption key.
@@ -101,7 +102,7 @@ public function run(array $params)
101102
// force DotEnv to reload the new env vars
102103
putenv('encryption.key');
103104
unset($_ENV['encryption.key'], $_SERVER['encryption.key']);
104-
$dotenv = new DotEnv(ROOTPATH);
105+
$dotenv = new DotEnv((new Paths())->envDirectory ?? ROOTPATH);
105106
$dotenv->load();
106107

107108
CLI::write('Application\'s new encryption key was successfully set.', 'green');
@@ -155,7 +156,7 @@ protected function confirmOverwrite(array $params): bool
155156
protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey): bool
156157
{
157158
$baseEnv = ROOTPATH . 'env';
158-
$envFile = ROOTPATH . '.env';
159+
$envFile = ((new Paths())->envDirectory ?? ROOTPATH) . '.env';
159160

160161
if (! is_file($envFile)) {
161162
if (! is_file($baseEnv)) {

system/Commands/Utilities/Environment.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\CLI\BaseCommand;
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Config\DotEnv;
19+
use Config\Paths;
1920

2021
/**
2122
* Command to display the current environment,
@@ -119,7 +120,7 @@ public function run(array $params)
119120
// however we cannot redefine the ENVIRONMENT constant
120121
putenv('CI_ENVIRONMENT');
121122
unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']);
122-
(new DotEnv(ROOTPATH))->load();
123+
(new DotEnv((new Paths())->envDirectory ?? ROOTPATH))->load();
123124

124125
CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
125126
CLI::write('The ENVIRONMENT constant will be changed in the next script execution.');
@@ -134,7 +135,7 @@ public function run(array $params)
134135
private function writeNewEnvironmentToEnvFile(string $newEnv): bool
135136
{
136137
$baseEnv = ROOTPATH . 'env';
137-
$envFile = ROOTPATH . '.env';
138+
$envFile = ((new Paths())->envDirectory ?? ROOTPATH) . '.env';
138139

139140
if (! is_file($envFile)) {
140141
if (! is_file($baseEnv)) {

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Changes
101101

102102
- **Cookie:** The ``CookieInterface::EXPIRES_FORMAT`` has been changed to ``D, d M Y H:i:s \G\M\T`` to follow the recommended format in RFC 7231.
103103
- **Format:** Added support for configuring ``json_encode()`` maximum depth via ``Config\Format::$jsonEncodeDepth``.
104+
- **Paths:** Added support for changing the location of the ``.env`` file via the ``Paths::$envDirectory`` property.
104105

105106
************
106107
Deprecations

user_guide_src/source/general/managing_apps.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,25 @@ of those:
9797
.. literalinclude:: managing_apps/004.php
9898

9999
Only when you change the Application Directory, see :ref:`renaming-app-directory` and modify the paths in the **index.php** and **spark**.
100+
101+
Changing the Location of the .env File
102+
======================================
103+
104+
If necessary, you can change the location of the ``.env`` file by adjusting the ``$envDirectory``
105+
property in ``app/Config/Paths.php``.
106+
107+
By default, the framework loads environment settings from a ``.env`` file located one level above
108+
the ``app/`` directory (in the ``ROOTPATH``). This is a safe location when your domain is correctly
109+
pointed to the ``public/`` directory, as recommended.
110+
111+
In practice, however, some applications are served from a subdirectory (e.g., ``http://example.com/myapp``)
112+
rather than from the main domain. In such cases, placing the ``.env`` file within the ``ROOTPATH`` may expose
113+
sensitive configuration data if ``.htaccess`` or other protections are misconfigured.
114+
115+
To avoid this risk in such setups, it is recommended that you ensure the ``.env`` file is located outside any web-accessible directories.
116+
117+
.. warning::
118+
119+
If you change the location of the ``.env`` file, make absolutely sure it is not publicly accessible.
120+
Exposure of this file could lead to compromised credentials and access to critical services, such as your
121+
database, mail server, or third-party APIs.

0 commit comments

Comments
 (0)