我有这个功能在我的控制器中不断被重用。我决定把它移到一个可以一直引用的文件中。这是我的文件结构
controllers
|Generic
|Users
|get_all_languages.php
|Users
|Lang
|Lang.php
我想引用包含的 get_all_languages
<?php
function get_all_languages(){
$this->curl->create(GetAllLanguages);
$this->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($this->curl->execute(),true);
}
到目前为止,我已经尝试将它包含在我的文件顶部,例如:
<?php
include __DIR__.'/../../Generic/Users/get_all_languages.php';
class Lang extends CI_Controller{
但是,当我尝试使用像 $this->get_all_languages(); 这样的函数时,会出现一个错误,说 Call to undefined method Lang::get_all_languages()
我也尝试在 __contruct 之后包含它,但它不允许我编译。
我希望有人能让我知道如何引用该函数。
谢谢你。
您可以使用codeigniter 的library
或helper
。
你可以在application/config/autoload.php
.(参考它) 中自动加载它们
如果你想要特定的控制器,你可以在使用$this->load->library()
或的控制器构造中使用它$this->load->helper()
。
例如:
class A extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('libraryname');
$this->load->helper('helpername');
}
public function index() {...}
...
}
...
更新
application/helpers/global_lang_helper.php
<?php
function get_all_languages(){
$CI = &get_instance();
$CI->load->library('curl');
$CI->curl->create(GetAllLanguages);
$CI->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($CI->curl->execute(),true);
}
在您的控制器...
public function __construct()
{
parent::__construct();
$this->load->helper('global_lang');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句