Codeigniter 4 - get instance in helper function
Asked Answered
S

5

6

how can i get in ci4 instance into helper function? $CI =&get_instance(); <- that was in version 3, how does this go in version Codeigniter 4?

Seligmann answered 10/4, 2020 at 10:8 Comment(0)
C
5

I made a gist for it. here is a way to do it.

  1. Create a helper file, you can name it whatever you want.
  2. Add the following codes to the helper you just created.

Content of the helper file: i.e: utility_helper.php

<?php

$CI_INSTANCE = [];  # It keeps a ref to global CI instance

function register_ci_instance(\App\Controllers\BaseController &$_ci)
{
    global $CI_INSTANCE;
    $CI_INSTANCE[0] = &$_ci;
}


function &get_instance(): \App\Controllers\BaseController
{
    global $CI_INSTANCE;
    return $CI_INSTANCE[0];
}
  1. Call register_ci_instance($this) at the very beginning of your base controller

  2. Now you may use get_instance() where ever you want just like CI3: $ci = &get_instance()

There are three notes I want to mention

  • I tested on PHP8 only.
  • I'm not sure if & is needed when calling get_instance. however, you have the option to use any form you want. so calling $ci = get_instance() is ok too, but you may do $ci = &get_instance() as you wish.
  • Make sure you change \App\Controllers\BaseController to something appropiate(i.e: your base controller). type hinting is great since IDEs understand them.
Clearing answered 28/6, 2021 at 10:23 Comment(0)
A
1

Create a common helper to keep the controller instance. Suppose here it is common_helper.php

$CI4 = new \App\Controllers\BaseController;

function register_CI4(&$_ci)
{
    global $CI4;
    $CI4 = $_ci;
}

In your BaseController

public $user_id;
protected $helpers = ['form', 'url', 'common']; // Loading Helper

public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
    parent::initController($request, $response, $logger);

    register_CI4($this); // Registering controller instance for helpers;
    $this->user_id = 4;
}

Now you will get the controller instance in all other helpers. Suppose a new heper user_helper as

function show_user_id()
{
    global $CI4;
    echo $CI4->user_id; // Showing 4
}

I tested it in PHP Version: 8.0.6

Aenea answered 15/11, 2021 at 7:44 Comment(0)
H
1

For me, this is the best solution for me:

if (!function_exists('getSegment'))
{

    /**
     * Returns segment value for given segment number or false.
     *
     * @param int $number The segment number for which we want to return the value of
     *
     * @return string|false
     */
    function getSegment(int $number)
    {
        $request = \Config\Services::request();

        if ($request->uri->getTotalSegments() >= $number && $request->uri->getSegment($number))
        {
            return $request->uri->getSegment($number);
        }
        else
        {
            return false;
        }
    }

} 

source: https://forum.codeigniter.com/thread-74755.html

Homing answered 15/7, 2022 at 1:25 Comment(0)
R
1

In CI4, just use the following:

$db = model('YourModel');

Regelate answered 23/8, 2023 at 7:1 Comment(0)
T
0

I get it (using CodeIgniter4):

in controller:

helper('login');      
$isLog=isLogged($this,"user","passwd");

if($isLog)
  ....
  else
  ....

in helper:

use App\Models\Loginmodel as Loginmodel;

function isLogged($ci,$e,$c){
    $ci->login = new Loginmodel;
    $ci->login->Like('paassword',$c);
    $id=$ci->login->select('id')->where('username',$e)->findAll();
    return $id;
}

I hope this helps.

Tidbit answered 21/5, 2020 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.