Welcome Guest, Not a member yet? Register   Sign In
Including CI4 resources in custom libraries
#1

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?
Reply
#2

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

(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.
Reply
#4

can you show example of usage please?
Reply
#5

(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)
Reply
#6

(This post was last modified: 04-10-2020, 11:13 AM by stlake2011.)

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB