[英]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.
这是一种方法。
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];
}
Call register_ci_instance($this)
at the very beginning of your base controller在你的基础 controller的最开始调用
register_ci_instance($this)
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我想提三个注意事项
&
is needed when calling get_instance
.get_instance
时是否需要&
。 however, you have the option to use any form you want.$ci = get_instance()
is ok too, but you may do $ci = &get_instance()
as you wish.$ci = get_instance()
也可以,但你可以根据需要调用 $ $ci = &get_instance()
。\App\Controllers\BaseController
to something appropiate( ie: your base controller ).\App\Controllers\BaseController
为适当的内容(即:您的基础 controller )。 type hinting is great since IDEs understand them.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.