Skip to content

Commit d5b28fd

Browse files
authored
Merge pull request #7401 from kenjis/docs-improve-environments
docs: improve environments
2 parents 354dc67 + cf2880b commit d5b28fd

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

user_guide_src/source/general/configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ One of today's best practices for application setup is to use Environment Variab
6363

6464
Environment Variables should also be used for anything private such as passwords, API keys, or other sensitive data.
6565

66+
.. _dotenv-file:
67+
6668
Environment Variables and CodeIgniter
6769
=====================================
6870

user_guide_src/source/general/environments.rst

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,44 @@ tools loaded that you don't in production environments, etc.
1111

1212
.. contents::
1313
:local:
14-
:depth: 2
14+
:depth: 3
15+
16+
************************
17+
The Defined Environments
18+
************************
19+
20+
By default, CodeIgniter has three environments defined.
21+
22+
- ``production`` for production
23+
- ``development`` for development
24+
- ``testing`` for PHPUnit testing
25+
26+
.. important:: The environment ``testing`` is reserved for PHPUnit testing. It
27+
has special conditions built into the framework at various places to assist
28+
with that. You can't use it for your development.
29+
30+
If you want another environment, e.g., for staging, you can add custom environments.
31+
See `Adding Environments`_.
32+
33+
*******************
34+
Setting Environment
35+
*******************
1536

1637
.. _environment-constant:
1738

1839
The ENVIRONMENT Constant
1940
========================
2041

21-
By default, CodeIgniter comes with the ``ENVIRONMENT`` constant set to use
22-
the value provided in ``$_SERVER['CI_ENVIRONMENT']``, otherwise defaulting to
23-
``production``. This can be set in several ways depending on your server setup.
24-
25-
.. note:: The environment ``testing`` is the special one for PHPUnit testing.
26-
It has special conditions built into the framework at various places to assist with that.
27-
You can't use it for your development.
42+
To set your environment, CodeIgniter comes with the ``ENVIRONMENT`` constant.
43+
If you set ``$_SERVER['CI_ENVIRONMENT']``, the value will be used,
44+
otherwise defaulting to ``production``.
2845

29-
.. note:: You can check the current environment by ``spark env`` command::
30-
31-
> php spark env
46+
This can be set in several ways depending on your server setup.
3247

3348
.env
3449
----
3550

36-
The simplest method to set the variable is in your :doc:`.env file </general/configuration>`.
51+
The simplest method to set the variable is in your :ref:`.env file <dotenv-file>`.
3752

3853
.. code-block:: ini
3954
@@ -88,8 +103,14 @@ In addition to affecting some basic framework behavior (see the next
88103
section), you may use this constant in your own development to
89104
differentiate between which environment you are running in.
90105

106+
*******************
107+
Adding Environments
108+
*******************
109+
110+
To add custom environments, you just need to add boot files for them.
111+
91112
Boot Files
92-
----------
113+
==========
93114

94115
CodeIgniter requires that a PHP script matching the environment's name is located
95116
under **APPPATH/Config/Boot**. These files can contain any customizations that
@@ -102,15 +123,32 @@ a fresh install:
102123
* production.php
103124
* testing.php
104125

126+
For example, if you want to add ``staging`` environment for staging, all you need
127+
to do is:
128+
129+
1. copy **APPPATH/Config/Boot/production.php** to **staging.php**.
130+
2. customize settings in **staging.php** if you want.
131+
132+
**********************************
133+
Confirming the Current Environment
134+
**********************************
135+
136+
To confirm the current environment, simply echo the constant ``ENVIRONMENT``.
137+
138+
You can also check the current environment by ``spark env`` command::
139+
140+
> php spark env
141+
142+
*************************************
105143
Effects on Default Framework Behavior
106-
=====================================
144+
*************************************
107145

108146
There are some places in the CodeIgniter system where the ``ENVIRONMENT``
109147
constant is used. This section describes how default framework behavior
110148
is affected.
111149

112150
Error Reporting
113-
---------------
151+
===============
114152

115153
Setting the ``ENVIRONMENT`` constant to a value of ``development`` will cause
116154
all PHP errors to be rendered to the browser when they occur.

0 commit comments

Comments
 (0)