As you might know, when you create a new project with CI you'll have to manually enter base url, encryption key in config/config.php. I'm trying to overcome this and is hence looking for a way to read those values from a database instead - making the installation for a customer and the set up time as a whole decrease a lot.
A customer isn't able to edit a PHP file's variables, but is most likely able to, with some guidance, enter base url and have a encryption key automatically filled in by the system.
Is there any way to accomplish this?
Of course! Add a hook - post_controller and set these config values through that file.
config/hooks.php
$hook['pre_controller'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
hooks/myotherclass.php
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
Basicly you set these values before they're used in any controller or similiar.
As far Codeigniter 3.1.7 pre_controller
won't work because it will return NULL value while initializing $CI =& get_instance();
To solve this:
Change the hook point from pre_controller
to post_controller_constructor
Modified source:
config/hooks.php
$hook['post_controller_constructor'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
and in hooks/myotherclass.php
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
Now it will work!
class name extends CI_Controller {
public $DB1;
public $MSSQL;
public function __construct()
{
parent::__construct();
$userid = $this->session->userdata('userids');
$this->load->model('kubix');
$result = $this->kubix->databaseconncetion($userid);
foreach ($result as $deta)
{
$this->MSSQL=$db['MSSQL'] = array(
'hostname' => $deta["Db_base_ip"],
'username' => $deta["Db_username"],
'password' => $deta["db_password"],
'database' => $deta["Db_base_name"],
'dbdriver' => 'sqlsrv',
'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);
$this->DB1 = $this->load->database($this->MSSQL, TRUE);
$sql = "SELECT TOP 1 * FROM AccountsFinancialYearMaster where Active=1 ";
$query = $this->DB1->query( $sql);
$result= $query->result_array();
RETURN $result;