在codeigniter中动态设置我的配置变量如何从其他控制器和模型访问它们? [英] After dynamically setting my config variable in codeigniter how to access them from other controllers and models?
问题描述
我已经在我的模型中更新我的配置变量使用:
I have updated my config variable in my model using:
$this->config->set_item('userid', $user_id);
如果我在模型中回声,我可以看到它设置。
if i echo it in the model i can see that it gets set.
但是如果我在控制器或其他模型中使用:
But if i echo it in the controller or another model using:
echo $this->config->item('userid');
它显示原始值。
我需要在整个会话中存储这个配置变量,但我不想使用会话变量。
I need to store this config variable throughout the session but i do not want to use session variables.
推荐答案
该配置仅适用于您设置的模型。如果您想要全局设置而不使用会话。
that configurtation is only applicable on that model which you set it. If you want a global setting without using session.
您可以在 application / core
it
MY_Model
c $ c> MY_Model do是它在所有扩展它的模型上设置你的配置。
so basically what MY_Model
do is that it sets your config on all models that extends it.
class MY_Model Extends CI_Model
{
protected $user_id;
public function __construct()
{
parent::__construct();
$this->config->set_item('userid', $this->user_id);
}
}
那么在你的模型上,你想要的设置应用,只是扩展你的模型。如
then on your model that you want the settings to be applied, just extends your model. like
Model extends MY_Model
{
public function test($id)
{
$this->user_id = $id;
}
}
或者您可以创建Core Controller同上,但替换控制器模型
阅读更多在 http:/ /ellislab.com/codeigniter/user-guide/general/core_classes.html
OR you could create a Core COntroller same as the above but substituting controller to model read more at http://ellislab.com/codeigniter/user-guide/general/core_classes.html
这篇关于在codeigniter中动态设置我的配置变量如何从其他控制器和模型访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!