【发布时间】:2015-12-28 18:17:08
【问题描述】:
假设我有一个像这样叫做 home 的控制器
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
//load the settings model
$this->load->Model('settings_model');
//get the all settings and store it into a variable
$siteSettings = $this->settings_model->SiteSettings();
//how to pass the data to view
$data['site'] = $siteSettings;
}
public function index()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod1()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
public function SomeMethod2()
{
//some code and pass the value to view
$data["some_item"] = "some value";
//Load the view
$this->load->view("home",$data);
}
}
但我的问题是我想将 $siteSettings 传递给我的每个方法。我不想每次都从 settings_model 获取数据,因为我使用不同的家庭控制器方法。我只想从 __construct() 上的数据库中获取数据,并在调用不同方法时将值传递给每个方法。
我怎样才能做到这一点?我应该使用公共变量并将获取的数据存储到该变量并从不同的方法调用该变量吗?
【问题讨论】:
-
is
$siteSettings是数组还是一个元素?? -
$siteSettings 是一个数组。
标签: php codeigniter