Welcome Guest, Not a member yet? Register   Sign In
get_instance() in pre_controller hook?
#11

[eluser]luismartin[/eluser]
Thank you guys! That's just what I needed to know. I only needed to use the URI and Config classes within the hook. I was trying to get it through the CI instance, but now I know how to manage without it.

FYI, hoping it helps other people in the future, this is what I implemented within the hook which was not working, and below it, the fix:

Code:
// code to execute if $_POST is not empty
        // get_instance gives me error because it tries to use the CI instance, which isn't available yet
        $CI =& get_instance();
        $path = $CI->config->item('cache_path');
        $cache_path = ($path == '') ? APPPATH . 'cache/' : $path;

        // algorithm to generate cache file name (same as internally in CI)
        $uri =  $CI->config->item('base_url').
            $CI->config->item('index_page').
            $CI->uri->uri_string();

        $cache_path .= md5($uri);

        if (file_exists($cache_path))
        {
            return unlink($cache_path);
        }
        else
        {
            return TRUE;
        }


Code:
// Manually load the desired core classes
        $CFG =& load_class('Config', 'core');
        $URI =& load_class('URI', 'core');        
                                
        $path = $CFG->item('cache_path');
        $cache_path = ($path == '') ? APPPATH . 'cache/' : $path;
        
        // algorithm to generate cache file name (same as internally in CI)
        $uri =  $CFG->item('base_url').
            $CFG->item('index_page').
            $URI->uri_string();

        $cache_path .= md5($uri);

        if (file_exists($cache_path))
        {
            return unlink($cache_path);
        }
        else
        {
            return TRUE;
        }

Only thing else apart from thank you for your help is #2 post should be corrected, since cache_override can't use the CI instance. That's what made me write this post.


Messages In This Thread
get_instance() in pre_controller hook? - by El Forum - 02-04-2011, 12:10 PM
get_instance() in pre_controller hook? - by El Forum - 02-04-2011, 09:14 PM
get_instance() in pre_controller hook? - by El Forum - 02-04-2011, 10:20 PM
get_instance() in pre_controller hook? - by El Forum - 02-05-2011, 07:10 AM
get_instance() in pre_controller hook? - by El Forum - 02-05-2011, 09:32 AM
get_instance() in pre_controller hook? - by El Forum - 01-23-2013, 04:06 PM
get_instance() in pre_controller hook? - by El Forum - 01-23-2013, 04:17 PM
get_instance() in pre_controller hook? - by El Forum - 01-23-2013, 05:44 PM
get_instance() in pre_controller hook? - by El Forum - 01-23-2013, 07:10 PM
get_instance() in pre_controller hook? - by El Forum - 01-23-2013, 07:21 PM
get_instance() in pre_controller hook? - by El Forum - 01-24-2013, 01:51 AM
get_instance() in pre_controller hook? - by El Forum - 02-01-2013, 04:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB