4

By default each codeigniter app has defined environment in the ci.php :

define('ENVIRONMENT', 'development');

I need to switch between different countries in my app, and each country has it`s own configuration and database. So I tried to create folder for each country in the 'config' folder and place there all the needed configs. Then I want to select country from a dropdown and switch the environment. Tried like this:

define('ENVIRONMENT', 'mexico');

but this raises error:

Message: Constant ENVIRONMENT already defined

How can I do this ?

Alexander Nikolov
  • 1,871
  • 7
  • 27
  • 49

6 Answers6

2

Constant: A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script

So there is no scope to redefine the value of ENVIRONMENT constant as it is already defined. You can use another constant name [except ENVIRONMENT or the other which are already used by codeignitor] or preferable a variable to maintain your country related version.

Community
  • 1
  • 1
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
1

In your root folder there is an file call index.php(image attach below)

01

In here if you define 'development', It will show your errors on your Codeigniter framework.

For example if database error occurs it shows this kind of error

enter image description here

So this you can check whats wrong and all.

You can test it by define to testing or production. So that you will only get blank page instead of Error Details.

and i there is no use of define a such variable call mexico

Read This Articles

  1. Handling Multiple Environments
  2. What is Development Environment?
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

You can try to make this in you index.php

switch( $_POST['env']) { case 'mexico' : define('ENVIRONMENT', 'mexico'); break; //etc ... }

wilson
  • 334
  • 1
  • 15
  • This will determine the config by url. I have only one url with many countries. So when select a country - switch to that country config. Maybe the approach is not correct, maybe I should not do this with environments. – Alexander Nikolov Aug 12 '15 at 11:57
  • Well, you can make this solution with `$_POST` variable, but it seems to me this is not a good practice to change environnement constant dynamically – wilson Aug 12 '15 at 12:00
0

Solution 1: Since ENVIRONMENT is already defined by default in codeigniter, therefore this specific need may achieve through

if (!defined('ENVIRONMENT')) {
    define('ENVIRONMENT', 'mexico');
}
Neoark Software
  • 281
  • 2
  • 5
0

Since ENVIRONMENT is already defined by default in codeigniter, therefore this specific need may achieve through

if (!defined('ENVIRONMENT')) {
    define('ENVIRONMENT', 'mexico');
}
Neoark Software
  • 281
  • 2
  • 5
-1

You may pass the selected country id or any short code in URL using get Method and use the below code in your index.php :

Your URL should be like :

www.example.com?country=IN OR

www.example.com/controller_name/function_name/IN

if ($_GET['country']=='IN')
   define('ENVIRONMENT', 'india');
elseif ($_GET['country']=='us')
   define('ENVIRONMENT', 'us');
else
   define('ENVIRONMENT', 'uk');
Harshal
  • 3,562
  • 9
  • 36
  • 65