0

I am a JS programmer who got thrown to PHP a few days ago and I'm trying to make a bit of sense. I've created a basic environment setup in FILE1 on Codeigniter for our server and choose the env on :

-----File1:----

$environments['default'] = array(
    'db_hostname' => '*****1.amazonaws.com',
    'base_url' => 'https://****co',
    'name' => 'default'
);

$environments['local'] = array(
    'db_hostname' => '*****2.amazonaws.com',
    'base_url' => 'https://local.****co',
    'name' => 'local'
);

-----File2:----
<?php
require_once __DIR__ .'/environments_config.php';
$env = $environments['local'];

So far so good,when I tried to use it on the config file in codeigniter it didn't work without the global keyword.

require_once __DIR__ .'/environment.php';
global $env; ///without global it doesn't work?

$config['base_url'] = $env['base_url'];

Maybe I'm missing something but shouldn't I declare a global variable only inside a function? because on File2 this setup works fine without a global declaration. Maybe its something related to Codeigniter that I'm missing?.

Vickel
  • 7,879
  • 6
  • 35
  • 56
Smiled_One
  • 445
  • 1
  • 4
  • 17
  • If you want to setup Database settings, you can do so in `application/config/database.php` file. For more reference please refer https://codeigniter.com/user_guide/database/configuration.html – G_real Oct 28 '19 at 13:40
  • 1
    Possible duplicate of [CodeIgniter - best place to declare global variable](https://stackoverflow.com/questions/17013397/codeigniter-best-place-to-declare-global-variable) – G_real Oct 28 '19 at 13:41
  • you can set global variables in `/application/config/constants.php` file. Reference https://stackoverflow.com/questions/17013397/codeigniter-best-place-to-declare-global-variable – G_real Oct 28 '19 at 13:44
  • Thanks everyone, the database file take the url from the env file. I wanted a single file only with the env that I won't need to change things between commits and just ignore it. I didn't put in const because I don't know in which order the things get load. I don't want the config.php file to reload before the environment – Smiled_One Oct 28 '19 at 13:54
  • notice that my question is why I do even need the global keyword in the firstplace – Smiled_One Oct 28 '19 at 13:55
  • 1
    it looks like somehow you bypass CI completely - thats not the correct way - take a look at https://codeigniter.com/user_guide/general/environments.html#configuration-files - you dont want to change that i get it - but anyway i strongly suggest to do that - because it saves a lot of headaches in the future. – Atural Oct 28 '19 at 15:22

0 Answers0