CodeIgniter Forums
how to: conditonal 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: how to: conditonal config (/showthread.php?tid=63514)



how to: conditonal config - starchild - 11-08-2015

Hi,

I want to be able to change the cache dir depending whether or not a suer is logged in.

I'm trying to do this from within the head.php view and have tried a bunch of methods including:

 $CI =& get_instance(); 
 if ($cust > 0) { 
 $config['cache_path'] = '/location/to/my/install/application/cache2';
 }


What is the correct syntax please?

Thanks a lot.


RE: how to: conditonal config - sintakonte - 11-09-2015

i think the best way to achieve what you want is a hook - below an example

in your application/config/hooks.php

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

in your application/hooks/ folder put a file named AppConfigCustomization.php

PHP Code:
class AppConfigCustomization
{

    private 
$ci;

    public function 
__construct()
    {
        
$this->ci = &get_instance();
    }
    
    
    public function 
initialize()
    {
        if (
$this->ci->session->userdata("is_customer"))
        {
            
$this->config->set_item("cache_path","/location/to/my/install/application/cache2");
        }
    }


something like this is clean and should work


RE: how to: conditonal config - starchild - 11-09-2015

Thanks a lot, I'll give this a go and let you know.


RE: how to: conditonal config - starchild - 11-11-2015

Bugger. didn't work.  Tried a few variations on this,  and it just wont overwrite the default cache path.


RE: how to: conditonal config - mwhitney - 11-13-2015

As long as you don't reload the config file after you do this, the following should work:

PHP Code:
$this->config->set_item('cache_path''/location/to/my/install/application/cache2'); 

http://www.codeigniter.com/user_guide/libraries/config.html#setting-a-config-item


RE: how to: conditonal config - starchild - 11-23-2015

*sigh* this is hurting my head.

I'm able to set cache path, but the problem now is that the session seems to be cached itself!
As soon as user logs in and caches the page to the assigned dir, even if I log out again, I'm still getting the logged in cache files being returned.

eg:

PHP Code:
$cust_id $this->session->userdata('cust_id');
        if (!isset(
$cust_id) || $cust_id <=) {
        
$this->config->set_item("cache_path","/path/to/application/cache3/") ;
        } else {
        
$this->config->set_item("cache_path","/path/to/application/cache2/");
        }
        
$this->output->cache(90); 

I guess the solution lies in environments, but how to assign an environment based on session data?  It all seems to be like a chicken and egg situation.