CodeIgniter Forums
Including CI4 resources in custom libraries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Including CI4 resources in custom libraries (/showthread.php?tid=75865)



Including CI4 resources in custom libraries - stlake2011 - 03-24-2020

Hey all,

Quick question:

How do you include CI4 resources (ie. Database, Session, etc etc) in a custom Library?
I know in CI3 you just included a reference to the Super Object then loaded the resource you needed, but as the docs are currently scarce on creating your own libraries in CI4 (unless i am missing them in my current copy I have), would I include CI4 resources/services the same as if I was including them in controllers and models?


RE: Including CI4 resources in custom libraries - kilishan - 03-24-2020

You got it - you create them just like you would in a controller/model.


RE: Including CI4 resources in custom libraries - stlake2011 - 03-24-2020

(03-24-2020, 07:05 PM)kilishan Wrote: You got it - you create them just like you would in a controller/model.
 Cool, Thank you.
Thought that may have been the way to do it. Just wanted to make sure.


RE: Including CI4 resources in custom libraries - igorc - 04-05-2020

can you show example of usage please?


RE: Including CI4 resources in custom libraries - Kaosweaver - 04-10-2020

(03-24-2020, 07:05 PM)kilishan Wrote: You got it - you create them just like you would in a controller/model.

PLEASE write that in the docs?

It would be super awesome if the docs got some attention with in context meaningful examples of using things in controllers, models, and views. Libraries, config files, helpers, etc - all could use some contextual examples to show off the correct way to access/call the thing and then utilize it.

I'm a long time CI user and would love to see the docs return to the helpfulness of v2 docs (which also had the wonderful slidedown panel with everything on it, making finding anything so much easier - I swear I spent the first minute or two trying to sort out which section of the docs anything belongs to)


RE: Including CI4 resources in custom libraries - stlake2011 - 04-10-2020

(04-10-2020, 08:51 AM)Kaosweaver Wrote:
(03-24-2020, 07:05 PM)kilishan Wrote: You got it - you create them just like you would in a controller/model.

PLEASE write that in the docs?

It would be super awesome if the docs got some attention with in context meaningful examples of using things in controllers, models, and views.  Libraries, config files, helpers, etc - all could use some contextual examples to show off the correct way to access/call the thing and then utilize it.

I'm a long time CI user and would love to see the docs return to the helpfulness of v2 docs (which also had the wonderful slidedown panel with everything on it, making finding anything so much easier - I swear I spent the first minute or two trying to sort out which section of the docs anything belongs to)

Here is a quick and dirty Custom Library and Controller that uses it example:

Custom Library:
PHP Code:
<?php namespace App\Libraries;

class 
CustomLib {

    protected $db;

    public function __construct()
    {
        $this->db = \Config\Database::connect();
    }

    public function CustomMethod($var1,$var2)
    {
        $sql "SELECT var1 as Var_1,var2 as Var_2 FROM table WHERE var1=? OR var2=?";

        return $result $this->db->query($sql,[$var1,$var2]);
    }


Controller using Library:

PHP Code:
<?php namespace App\Controllers;
use 
App\Libraries\CustomLib;

class 
CustomerController extends BaseController {

    protected $customLib;

    public function __construct()
    {
        $this->customLib = new CustomLib();
    }

    public function index()
    {
        $data['sumdata'] = $this->customLib->CustomMethod($v1,$v2);

        return view('sum_page',$data)
    }


as you can see, its quite easy actually compared to CI3.