Welcome Guest, Not a member yet? Register   Sign In
how to: conditonal config
#1

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.
Reply
#2

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
Reply
#3

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

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

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/li...onfig-item
Reply
#6

(This post was last modified: 11-23-2015, 08:37 PM by starchild.)

*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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB