Welcome Guest, Not a member yet? Register   Sign In
Call CI library in my hook
#1

[eluser]Marcus Cavalcanti[/eluser]
How to use CI Library in my Hook?

I tried this:

Code:
class Newsletter {
        
        function setInformation () {
            $this->CI =& get_instance();            
            $this->CI->load->library('validation');

            $fields['name']  = 'Nome';
            $fields['email'] = 'Email';        
            $this->CI->validation->set_fields($fields);
            
            
            $rules['name']      = "required|xss_clean";
            $rules['email']      = "required|valid_email|xss_clean";
            $this->CI->validation->set_rules($rules);
            
            
            $this->CI->validation->set_message('required', string_to_entities("Campo obrigatório."));
            $this->CI->validation->set_message('valid_email', string_to_entities("Email inválido."));
        }
        
    }

But the error occurs:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: hooks/Newsletter.php

Line Number: 8

Fatal error: Call to a member function library() on a non-object in /var/www/vhosts/loogica.redirectme.net/httpdocs/marcus/CI3/system/application/frontend/hooks/Newsletter.php on line 8
#2

[eluser]Unknown[/eluser]
Any luck with this kind of action?
#3

[eluser]instantations[/eluser]
I'm having the exact same problem. :-S Can't it be done and how?
#4

[eluser]alexsancho[/eluser]
can you post your hooks.php config?
#5

[eluser]gunter[/eluser]
turn logging on in your config.php

and you will see reading the log file if your hook is called before the autoloaded libraries are ready - or not...
#6

[eluser]instantations[/eluser]
[quote author="instantations" date="1193360663"]I'm having the exact same problem. :-S Can't it be done and how?[/quote]

I've solved my problem. It seems you can't instantiate CI in a pre-controller hook, but you can do so in a post_controller_constructor hook and this is soon enough in the CI flow to do what I wanted to do (log a user automatically from a "remember me" cookie).
#7

[eluser]adamp1[/eluser]
Im having the same problem. I am writing a hook to check the browser language and thus change the $config['language'] value accordingly. The problem is I need to call the hook before my controller methods run, because after this the output has been generated and its too late to apply a different language file. How on earth can I do this then? It said for pre_controller that all core classes were loaded but I can't access them using $this->CI = &get;_instance();

Any ideas would be most welcome.
#8

[eluser]tonanbarbarian[/eluser]
If you are using a pre_system hook the get_instance will probably not even be available.
If you are using a pre_controller hook the get_instance function may be available, but the controller has not been instanciated yet so get_instance will probably not reeturn anything meaningful
It is only once you get to the post_controller_constructor hook and beyond that you can actually do anything from the controller passed by get_instance.

Note: I am not 100% sure on this because I have not used hooks as yet but in investigating hooks and how they work this is what I have gathered from the user guide and looking at the code. If you wan to know more I suggest you look at the code in system/codeigniter/CodeIgniter.php and you should be able to follow what it is doing in a general way including where it is calling hooks and where it is creating the controller (not the load_class('Controller', FALSE) only loads the code for the controller it does not create an instance of the controller.

I think the user guide is quite good on this, however if it gave actual examples of the objects available at each stage it might make it easier for first time hook coders to determine where to put a hook and what they can access




Theme © iAndrew 2016 - Forum software by © MyBB