Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access which environment is set in CodeIgniter?

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?

like image 470
S.K Avatar asked Aug 27 '14 09:08

S.K


People also ask

Where is ENV file in CodeIgniter?

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 (.)

Which file contains the environment constant in CodeIgniter?

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.

What is config file in CodeIgniter?

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.


2 Answers

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.

like image 107
TunaMaxx Avatar answered Oct 22 '22 16:10

TunaMaxx


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; 
like image 27
Girish Avatar answered Oct 22 '22 16:10

Girish