简体   繁体   English

Codeigniter 4 - 在助手 function 中获取实例

[英]Codeigniter 4 - get instance in helper function

how can i get in ci4 instance into helper function?我怎样才能将 ci4 实例放入助手 function? $CI =&get_instance(); $CI =&get_instance(); <- that was in version 3, how does this go in version Codeigniter 4? <- 那是在版本 3 中,这个 go 在版本 Codeigniter 4 中如何?

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: ie: utility_helper.php帮助文件内容:即: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在你的基础 controller的最开始调用register_ci_instance($this)

  2. Now you may use get_instance() where ever you want just like CI3: $ci = &get_instance()现在你可以像 CI3 一样在任何你想要的地方使用get_instance()$ci = &get_instance()

There are three notes I want to mention我想提三个注意事项

  • I tested on PHP8 only.我只在PHP8上测试过。
  • I'm not sure if & is needed when calling get_instance .我不确定在调用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.所以调用$ci = get_instance()也可以,但你可以根据需要调用 $ $ci = &get_instance()
  • Make sure you change \App\Controllers\BaseController to something appropiate( ie: your base controller ).确保将\App\Controllers\BaseController为适当的内容(即:您的基础 controller )。 type hinting is great since IDEs understand them.类型提示很棒,因为 IDE 可以理解它们。

I get it (using CodeIgniter4):我明白了(使用 CodeIgniter4):

in controller :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.我希望这有帮助。

Create a common helper to keep the controller instance.创建一个通用助手来保留 controller 实例。 Suppose here it is common_helper.php假设这里是common_helper.php

$CI4 = new \App\Controllers\BaseController;

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

In your BaseController在你的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.现在您将在所有其他助手中获得 controller 实例。 Suppose a new heper user_helper as假设一个新的 heper user_helper

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

I tested it in PHP Version: 8.0.6我在PHP Version: 8.0.6

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来源: https://forum.codeigniter.com/thread-74755.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM