在codeigniter中动态设置我的配置变量后,如何从其他控制器和模型访问它们


After dynamically setting my config variable in codeigniter how to access them from other controllers and models?

我已经更新了我的配置变量在我的模型使用:

$this->config->set_item('userid', $user_id);

如果我在模型中回显它,我可以看到它被设置了。

但是如果我在控制器或其他模型中使用:

echo $this->config->item('userid');

显示原始值

我需要存储这个配置变量在整个会话,但我不想使用会话变量。

该配置仅适用于您设置的模型。如果你想要一个全局设置而不使用session.

你可以在application/core上创建一个核心模型并命名为

MY_Model

所以基本上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;
   }
}

或者你可以创建一个与上述相同的核心控制器,但将控制器替换为模型参阅Codeigniter扩展核心类

尝试初始化配置$this->ci = & get_instance();,然后设置如下值$this->ci->config->set_item('userid', $user_id);

我还没有测试过这个…