Custom Library Question |
[eluser]wowdezign[/eluser]
I am building a custom library and was wondering if it is possible to load and use a native CI library from within my custom library. I have searched and cannot find an example. I tried it myself and I couldn't seem to get it working. If anyone has an example, I appreciate a brief description of how to do it. Thanks, Matt
[eluser]Ben Edmunds[/eluser]
Yes, you have to instantiate the CI super-object. In your library simple do Code: protected $ci; Then to use native CI function simple do Code: $this->ci->function(); like Code: $this->ci->load->model('your_model');
[eluser]wowdezign[/eluser]
Thanks Ben for the reply. That's what I thought after reading the documentation. However, when I tried to load the validation library into my custom Authorization object, I got errors. In your response, you give the example of loading a "Model". I have done that and it works fine. But what I want to do is load a "Library" into another "Library". This is what I have not been able to do.
[eluser]Ben Edmunds[/eluser]
I've loaded libraries from libraries fine before so I'm not exactly sure what the problem is. Just make sure that you are using the $this-ci->library syntax. and not just $this->library. If that's not it post some code and we'll see what's up.
[eluser]wowdezign[/eluser]
OK, I got it working. I was loading the form_validation library inside my custom library like: Code: $this->ci->load->('form_validation'); I just figured that I could set the validation rules inside my library like: Code: $this->ci->form_validation->set_rules('user_username','Username','rule string here'); and then do the test in my controller: Code: if($this->form_validation->run() == FALSE){ Apparently there was a problem with doing it this way. I don't know why (perhaps I wasn't referencing the same object or something), but when I moved this code block: Code: if($this->form_validation->run() == FALSE){ into my custom library the problems went away. If someone knows why I wasn't able to set the rules in my library and run the validation test in the controller, I would appreciate the information. It would help me understand the architecture better. Thanks again! Matt
[eluser]Ben Edmunds[/eluser]
I would assume it is because you are running a different instance in your library than in your controller but I'm not exactly sure how get_instance() is wrote so I can't say for sure.
[eluser]wowdezign[/eluser]
I'm assigning the ci object by reference in my library like the documentation says to. Are you referring to assigning the form_validation library to an instance by reference? I just do: Code: $this->ci =& get_instance(); and then when I reference any other library, I go through that ci instance like: Code: $this->ci->form_validation->set_rules(); etc. Maybe I misunderstood, but I thought that once the reference to the CI super class was made as a property of the object, I could refer to it from anywhere.
[eluser]InsiteFX[/eluser]
Take a look Adam Griffiths Auth 1.0.6 Library he does the validation in it. Programmers Voice Enjoy InsiteFX |
Welcome Guest, Not a member yet? Register Sign In |