Welcome Guest, Not a member yet? Register   Sign In
Custom library in CI, rudimentary question
#1

[eluser]stevefink[/eluser]
Hi all,

Just trying to understand the internals of CI a bit better here.

I created a custom authorization class for usage with CI which I placed in system/application/libraries folder. For the purpose of this example, let's say my class is named Auth.

Here's an example of what I put in the libraries directory, file named Auth.php

Code:
class Auth {

   var $CI;

   function Auth()
   {
      $this->CI =& get_instance()
   }

   function foo()
   {
      echo "Testing custom lib!";
   }
}

My controller looks like the following:

Code:
function index()
{
   $this->load->library('auth');
}

function bar()
{
   $this->auth->foo();
}

Is there any reason why that won't work? I had to move the library loading into the bar() function and call Auth::foo() to see my method get invoked. I'm curious what is possibly done not in the conventional way that is breaking the code above (although no errors are being spat out.)

Thanks all.

/sf
#2

[eluser]stevefink[/eluser]
Ah I'm ashamed of myself. It's been a few months since I wrote a CI application and for some reason I thought the engine was written to see function index() as the constructor for one odd reason or another. Silly me. :-)

I had to add a proper constructor and also call parent::Controller() and it works now.

Wish I could delete my own post. ;-)

/sf




Theme © iAndrew 2016 - Forum software by © MyBB