I am new to CodeIgniter. I have found that, in order to manage multiple environments, CodeIgniter uses the following function in index.php
define('ENVIRONMENT', 'development');
to define the environment.
My question is, how can I get which environment set at index.php inside my controllers?
CodeIgniter expects . env to be at the root of your project alongside the app directories. There is a template file distributed with CodeIgniter that's located at the project root named env (Notice there's no dot (.)
CodeIgniter requires that a PHP script matching the environment's name is located under APPPATH/Config/Boot. These files can contain any customizations that you would like to make for your environment, whether it's updating the error display settings, loading additional developer tools, or anything else.
The Config class provides a means to retrieve configuration preferences. These preferences can come from the default config file (application/config/config. php) or from your own custom config files. Note. This class is initialized automatically by the system so there is no need to do it manually.
In your index.php file, try something like this:
if ($_SERVER['HTTP_HOST'] == 'dev' || $_SERVER['HTTP_HOST'] == 'localhost')
{
define('ENVIRONMENT', 'development');
}
elseif ($_SERVER['HTTP_HOST'] == 'staging.example.com')
{
define('ENVIRONMENT', 'staging');
}
else
{
define('ENVIRONMENT', 'production');
}
Obviously, set it up with values that make sense for you. However, this will set the ENVIRONMENT based on where the application is running, automatically.
ENVIRONMENT
is defined in index.php
that is pipeline
of each CI application file, you can access anywhere e.g model, view, controller, library
echo ENVIRONMENT;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With