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

[eluser]slowgary[/eluser]
Thanks in advance for reading.

I'm trying to get access to the CI object in a pre_controller hook, but calling get_instance returns NULL. Any ideas?

My application uses a variable number of URI segments, but now needs to allow a query string for Google Analytics. Adding the query string causes woes in my application. What I'd like to do is strip any query strings from CodeIgniter's uri_string variable (and any segment variables) in a hook before my application tries to use any of them.

Why can't I access $CI in the hook? Is there a better way to get the job done?


Thank you!
#2

[eluser]InsiteFX[/eluser]
You can not get the CI instance in a hook, because CI is not
loaded yet!

InsiteFX
#3

[eluser]vitoco[/eluser]
@InsiteFX, this is not entirely correct, you can't get the CI instance in SOME hooks :

- CAN'T : pre_system , pre_controller
- CAN : post_controller_constructor, post_controller, display_override, cache_override, post_system

Saludos

EDIT : @slowgary i think you can call directly to the instances of the core classes, like $URI, $RTR, etc...that are created in system/core/Codeigniter.php
#4

[eluser]InsiteFX[/eluser]
Hi vitoco,

Thanks for the carification.

You are right, I re-checked the codeigniter.php file and it is loading
the pre_system and pre_controller hooks then assigning the instance then it
loads the post_controller_constructor.

InsiteFX
#5

[eluser]slowgary[/eluser]
I actually solved (most of) the problem without a hook, since it needed to happen early on in my application, not in any of the post hooks.

I'm already using a BaseController class in MY_Controller, so I just tossed a few preg_replaces in the constructor of BaseController, which strip anything starting with a question mark from $this->uri->uri_string and the last segment of both $this->uri->segments and $this->uri->rsegments.

Now the only problem that remains is query strings on the root domain, e.g. www.mydomain.com?utm_campaign=yadayada.

Since it has nothing to do with the URI string at that point, but still confuses CI, it breaks the app. When I figure out how to overcome this I'll post a reply for the next guy.

InSite, Vitoco - Thanks for your help!
#6

[eluser]luismartin[/eluser]
This thread has about a year, but there's something I'd like to say, and a question to add.

I'm having problems to get the CI instance within a cache_override hook. I can use the get_instance() function, but it fails when trying to get the CI instance within this function, and throws this error:

Fatal error: Class 'CI_Controller' not found in C:\mysite\system\core\CodeIgniter.php on line 233

So, it seems that CI is not available for this hook.

This is quite an annoyance for me, since I've implemented a web with a fixed contact form in the footer, and I was using the CI output cache. When someone sends a contact message with this form, the data is processed and the e-mail is sent, returning to the same page and displaying a message in place of the form. But if this page is already cached, the message won't be processed.

So I thought on using this hook to delete the cached page which corresponds to the current uri request everytime the form is sent, so as to force processing everything (form data included). However I've run into this problem I can't solve right now.

I'd really appreciate if you or anyone else pointed me to a solution.
#7

[eluser]InsiteFX[/eluser]
Are you using a pre or post hook?

[code]
private $CI;

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

parent::__construct();
}
[code]
#8

[eluser]luismartin[/eluser]
I'm using the cache_override hook. Does it imply pre or post?
#9

[eluser]InsiteFX[/eluser]
The cache_override hook is loaded before CI and the other hooks so you can not get the CI Instance.
You would need to use a post_controller hook for what you are trying to do.
#10

[eluser]vitoco[/eluser]
from what i see on core/Codeigniter.php the following code check if there's a cache_override hook

Code:
// Line ~193
if ($EXT->_call_hook('cache_override') === FALSE)

and as insitefx said, still not available the $CI object, but this classes are initialized ( that later will be assigned to $CI, and can be called like $this->class )
Code:
$EXT =& load_class('Hooks', 'core');
$CFG =& load_class('Config', 'core');
$UNI =& load_class('Utf8', 'core');
$URI =& load_class('URI', 'core');
$RTR =& load_class('Router', 'core');
$OUT =& load_class('Output', 'core');

so you can access them with

Code:
//core/Common.php
// function &load;_class($class, $directory = 'libraries', $prefix = 'CI_') ;
$RTR = &load;_class('Router');

and use it as any other object ( or as if there were part of $this->class )

Slds




Theme © iAndrew 2016 - Forum software by © MyBB