Welcome Guest, Not a member yet? Register   Sign In
Including CI4 resources in custom libraries
#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


Messages In This Thread
RE: Including CI4 resources in custom libraries - by stlake2011 - 04-10-2020, 11:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB