CodeIgniter 4 – Creating your own libraries

Creating your own library in CodeIgniter 4 is just as simple as in CI3 but the syntax has changed and there’s no documentation yet so here is my approach.

In CI3 your class has a basic prototype:

class Someclass {
    public function some_method()
    {
    }
}

It goes into the /app/libraries folder and is loaded in the controller:

$this->load->library('someclass');

Your class is now ready to be used.

In CodeIgniter 4, the path is similar: /app/Libraries and your class needs a prototype with an namespace:

namespace App\Libraries;
class Someclass {
    public function some_method()
    {
    }
}

You load it in your controller with:

$someclass = new \App\Libraries\Someclass();

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.