• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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?

    • ENVIRONMENT itself is the constant, its like a variable so you can save it in a variable or use it directly like : $evn = ENVIRONMENT

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; 
  • 13
Reply Report

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.

  • 3
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions