Php 从CodeIgniter中每个页面都需要的模型中检索数据的最佳方法是什么?
当用户登录到我的CodeIgniter应用程序时,我需要一个配置文件列表,他们必须在他们访问的每个页面的菜单中列出这些配置文件。而不是像这样对应用程序中的每个方法调用模型函数:Php 从CodeIgniter中每个页面都需要的模型中检索数据的最佳方法是什么?,php,mysql,codeigniter,caching,Php,Mysql,Codeigniter,Caching,当用户登录到我的CodeIgniter应用程序时,我需要一个配置文件列表,他们必须在他们访问的每个页面的菜单中列出这些配置文件。而不是像这样对应用程序中的每个方法调用模型函数: if ($this->users_model->is_logged_in()) { $data->profiles = $this->profiles_model->get_profiles(); } $this->load->vars($data); 有更好的方法吗
if ($this->users_model->is_logged_in())
{
$data->profiles = $this->profiles_model->get_profiles();
}
$this->load->vars($data);
有更好的方法吗?我也考虑过像这样扩展CI_Controller类的选项,但无法确定如何最好地将变量传递给需要信息的实际方法
class MY_Controller extends CI_Controller
{
public function __construct()
{
// Alternatively these could be auto-loaded
$this->load->model(array('profiles_model', 'users_model'));
if ($this->users_model->is_logged_in())
{
$data->profiles = $this->profiles_model->get_profiles();
}
}
}
我关心的是最佳实践、最佳性能和最佳缓存能力(如果可能的话)。如果我在所有页面上都有需要的内容,我会执行以下操作
class MY_Session extends CI_Session {
public function __construct() {
parent::__construct();
}
public function login($email, $password) {
$CI =& get_instance();
$CI->load->model('usermodel', 'user');
$result = $CI->user->login($email, $password);
if($result === false) {
return false;
} else {
$this->set_userdata('email', $result->email);
return true;
}
}
public function is_logged_in() {
$email = $this->userdata('email');
if(!empty($email)) {
return true;
} else {
return false;
}
}
}