Welcome Guest, Not a member yet? Register   Sign In
[Solved] Database Driven Config Question
#1

(This post was last modified: 08-19-2017, 04:36 PM by wolfgang1983.)

I am working on a config file that I get my contents from database and set them in my config

application/config/setting.php

As you can see below I can require the database file fine,

Is there any way to be able to require codeigniter URI segments

Because currently I have to get segments like this  so would be better if I could use the url in my config file any ideas how to load url helper in my config file below 

cheers.


PHP Code:
$segments explode('/'trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
$numSegments count($segments); 
$currentSegment 
$segments[$numSegments 3]; 

PHP Code:
<?php

$config 
= array();

require_once(
BASEPATH 'database/DB.php');

$db =& DB();

$db->where('type''config');
$query $db->get($db->dbprefix 'setting');

foreach (
$query->result_array() as $result) {
    
$config[$result['item']] = $result['value'];
}

$segments explode('/'trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
$numSegments count($segments); 
$currentSegment $segments[$numSegments 3];

if (
$currentSegment == 'admin') {
    
$db->where('code'config_item('admin_language'));
} elseif (
$currentSegment != 'admin') {
    
$db->where('code'config_item('language'));
}

$lang_query $db->get($db->dbprefix 'language');

if (
$query->num_rows() > 0) {
    
$config['language']    = strtolower($lang_query->row()->name);
} else {
    
$config['language']    = 'english';

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 08-17-2017, 04:43 AM by salain.)

Hi,

You could use a post_controller_constructor hook
Where you have access to CI super object.

Hook.php
PHP Code:
$hook['post_controller_constructor'] = array(
 
       'function' => 'config_hook',
 
       'filename' => 'hook_config.php',
 
       'filepath' => 'hooks'
 
   ); 

hook_config.php
PHP Code:
function config_hook()
{
 
   $CI =& get_instance();
 
   $uri_string $CI->uri->uri_string();
 
   $CI->db->where('field''value');
 
   $query $CI->get('table');
 
   // etc..
 
   


Don't forget to enable hooks in config.php
A good decision is based on knowledge and not on numbers. - Plato

Reply
#3

(This post was last modified: 08-19-2017, 05:28 PM by wolfgang1983.)

Thanks for the help all.

I load my hooks now like


PHP Code:
$hook['pre_controller'][] = array(
        'class'    => 'Language',
        'function' => 'set',
        'filename' => 'Language.php',
        'filepath' => 'hooks'
); 

$hook
['pre_controller'][] = array(
        'class'    => 'Setting',
        'function' => 'set',
        'filename' => 'Setting.php',
        'filepath' => 'hooks'
); 


And  my language hook

PHP Code:
<?php

class Language {

    public function 
set()
    {
        
$this->CI =& get_instance();

        
$this->CI->load->library('site');

        if (
$this->CI->uri->segment(1) == 'admin')
        {
            
$this->CI->db->where('code'$this->CI->site->get('admin_language'));
            
$query $this->CI->db->get($this->CI->db->dbprefix 'language');

            
$this->CI->config->set_item('admin_language'strtolower($query->row()->name));

            
$this->CI->config->set_item('language'strtolower($query->row()->name));

            
$this->CI->lang->load('admin/general'strtolower($query->row()->name));
        }
    }



And my setting hook

PHP Code:
<?php

class Setting {

    public function 
set()
    {
        
$this->CI =& get_instance();

        
$this->CI->db->where('item''url');
        
$query $this->CI->db->get($this->CI->db->dbprefix 'setting');

        
$this->CI->config->set_item('base_url'$query->row()->value);
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB