Search code examples
phpoopkohana-3autoloader

How use custom libraries in Kohana 3


I am trying to include a user library that includes some user related functions such as checking if the user is authenticated and such. Now I am trying to make usage of the Kohana autoloader, but can't seem to get it working.

I have the library placed under application/classes/library

class User {
 public function is_alive()
 {
   $session = Session::instance();
   $data = $session->get('alive');

   if(isset($data))
   {
    return true;
   }
   else
   {
    return false;
   }
 }
}

And I try to call the library with

$user = new User;

But it doesn't seem to do the trick.

How can I call a custom library?


Solution

  • I have the library placed under application/classes/library

    Place the library in /application/classes/.

    Otherwise, you have to place this in your controller:

    public function before() {
        require Kohana::find_file('classes', 'library/User');
    }
    

    You can read about this here.

    Now you can do the same as before, with User.php inside the directory library.