Skip to content

Commit a43018a

Browse files
authored
Merge pull request #7454 from kenjis/docs-improve-env-var-and-config
docs: improve description on Env Vars and Config values
2 parents f89ec81 + 296b785 commit a43018a

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

user_guide_src/source/database/configuration.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ default group's configuration settings. The values should be name following this
104104
database.default.password = '';
105105
database.default.database = 'ci4';
106106

107+
But you cannot add a new property by setting environment variables, nor change a
108+
scalar value to an array. See :ref:`env-var-replacements-for-data` for details.
109+
110+
So if you want to use SSL with MySQL, you need a hack. For example, set the array
111+
values as a JSON string in your **.env** file:
112+
113+
::
114+
115+
database.default.encrypt = {"ssl_verify":true,"ssl_ca":"/var/www/html/BaltimoreCyberTrustRoot.crt.pem"}
116+
117+
and decode it in the constructor in the Config class:
118+
119+
.. literalinclude:: configuration/009.php
120+
107121
**********************
108122
Explanation of Values:
109123
**********************
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Database\Config;
6+
7+
/**
8+
* Database Configuration
9+
*/
10+
class Database extends Config
11+
{
12+
// ...
13+
14+
public function __construct()
15+
{
16+
// ...
17+
18+
$array = json_decode($this->default['encrypt'], true);
19+
if (is_array($array)) {
20+
$this->default['encrypt'] = $array;
21+
}
22+
}
23+
}

user_guide_src/source/general/configuration.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ Configuration Classes and Environment Variables
150150
When you instantiate a configuration class, any *namespaced* environment variables
151151
are considered for merging into the configuration object's properties.
152152

153+
.. important:: You cannot add a new property by setting environment variables,
154+
nor change a scalar value to an array. See :ref:`env-var-replacements-for-data`.
155+
153156
If the prefix of a namespaced variable exactly matches the namespace of the configuration
154157
class, then the trailing part of the setting (after the dot) is treated as a configuration
155158
property. If it matches an existing configuration property, the environment variable's
@@ -180,20 +183,26 @@ Since v4.1.5, you can also write with underscores::
180183

181184
.. note:: When using the *short prefix* the property names must still exactly match the class defined name.
182185

186+
.. _env-var-replacements-for-data:
187+
183188
Environment Variables as Replacements for Data
184189
==============================================
185190

186191
It is very important to always remember that environment variables contained in your **.env** are
187-
**only replacements for existing data**. This means that you cannot expect to fill your **.env** with all
188-
the replacements for your configurations but have nothing to receive these replacements in the
189-
related configuration file(s).
192+
**only replacements for existing data**.
193+
194+
Simply put, you can change only the property value that exists in the Config class
195+
by setting it in your **.env**.
196+
197+
You cannot add a property that is not defined in the Config class, nor can you
198+
change it to an array if the value of the defined property is a scalar.
190199

191-
The **.env** only serves to fill or replace the values in your configuration files. That said, your
192-
configuration files should have a container or receiving property for those. Adding so many variables in
193-
your **.env** with nothing to contain them in the receiving end is useless.
200+
For example, you cannot just put ``app.myNewConfig = foo`` in your **.env** and
201+
expect your ``Config\App`` to magically have that property and value at run time.
194202

195-
Simply put, you cannot just put ``app.myNewConfig = foo`` in your **.env** and expect your ``Config\App``
196-
to magically have that property and value at run time.
203+
When you have the property ``$default = ['encrypt' => false]`` in your
204+
``Config\Database``, you cannot change the ``encrypt`` value to an array even if
205+
you put ``database.default.encrypt.ssl_verify = true`` in your **.env**.
197206

198207
Treating Environment Variables as Arrays
199208
========================================

0 commit comments

Comments
 (0)