CodeIgniter Forums
$this->uri->segment() in config - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: $this->uri->segment() in config (/showthread.php?tid=75951)



$this->uri->segment() in config - pippuccio76 - 04-01-2020

HI it's possible change the database config by $this->uri->segment() :

Code:
$active_group = 'default';
$query_builder = TRUE;

if ($this->uri->segment(1)=='my_domain_1') {

    $db['default'] = array(
        'dsn'    => '',
        'hostname' => 'localhost',
        'username' => '**********',
        'password' => '***********',
        'database' => '***********',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );    
}elseif($this->uri->segment(1)=='my_domain_2'){

    $db['default'] = array(
        'dsn'    => '',
        'hostname' => 'localhost',
        'username' => '********',
        'password' => '********',
        'database' => '********',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );



}


I have this error Message: Using $this when not in object context


RE: $this->uri->segment() in config - InsiteFX - 04-01-2020

$this is a pseudo-variable (also a reserved keyword) which is only available inside methods. And,
it refers to the object of the current method. Which database.php is not an object (Class).