Php 在codeigniter中动态设置我的配置变量后,如何从其他控制器和模型访问它们?
我已使用以下工具更新了模型中的配置变量:Php 在codeigniter中动态设置我的配置变量后,如何从其他控制器和模型访问它们?,php,codeigniter,config,Php,Codeigniter,Config,我已使用以下工具更新了模型中的配置变量: $this->config->set_item('userid', $user_id); echo $this->config->item('userid'); 如果我在模型中回显它,我可以看到它被设置好了 但如果我在控制器或另一个模型中使用: $this->config->set_item('userid', $user_id); echo $this->config->item('userid')
$this->config->set_item('userid', $user_id);
echo $this->config->item('userid');
如果我在模型中回显它,我可以看到它被设置好了
但如果我在控制器或另一个模型中使用:
$this->config->set_item('userid', $user_id);
echo $this->config->item('userid');
它显示原始值
我需要在整个会话中存储此配置变量,但我不想使用会话变量 该配置仅适用于您设置的型号。如果希望在不使用会话的情况下进行全局设置 您可以在
应用程序/core
上创建一个核心模型并命名它
myu车型
所以基本上,MY_Model
所做的就是在所有扩展它的模型上设置您的配置
class MY_Model Extends CI_Model
{
protected $user_id;
public function __construct()
{
parent::__construct();
$this->config->set_item('userid', $this->user_id);
}
}
然后在您希望应用设置的模型上,只需扩展您的模型。像
Model extends MY_Model
{
public function test($id)
{
$this->user_id = $id;
}
}
或者您可以创建一个与上面相同的核心控制器,但将控制器替换为模型
阅读更多信息,请尝试初始化配置
$this->ci=&get_instance()
然后设置值,如下所示
$this->ci->config->set_项('userid',$user_id)代码>
我还没有测试过这个。。。但是我想它会起作用。嘿,谢谢:)它起作用了。我已经创建了一个核心模型并使用了您的逻辑。我还想知道一件事,现在我是否必须将所有其他模型类也扩展到我的_模型???@Simone是的,你需要,因为核心模型包含你需要的全局配置项。我已经测试过了。。使用$ci=&get_instance();和$ci->config->set_项('userid',$user_id);