4

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?

1
  • Zar, since version 2 you don't have to enter a base URL in the config file at all. Also, you can't start a session without an encryption key, so you might as well write the value to file with an installation script that doesn't require a session/login, which then means you may not need to read config from the DB at all. Commented May 20, 2012 at 15:31

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess it'd be a preformance boost to cache these values? Thanks!
Well ye, but it's basicly just a sole simple query run on each page load. I doubt you will get to the point where this would be worth caching, since the amount of sites may not extend millions? :) It's worth saving gained result to a global value, so you can reuse it for other parts of your site.
@Zar: You should just write the values directly to file/cache as soon as they are saved by the user. You really don't need the DB for this at all.
3

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!

1 Comment

post controller is useless if you want to use these config settings in controller. because from your method it won't set config until it finish executing controller code.
0
 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;     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.