数据可用于 codeigniter 中的所有视图 [英] Data available for all views in codeigniter

查看:18
本文介绍了数据可用于 codeigniter 中的所有视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量,包含应该存在于整个站点中的数据.我没有将这些数据传递给每个控制器的每个视图,而是想知道是否有办法让这些数据可用于站点中的每个视图.

I have a variable, contaning data that should be present in the entire site. Instead of passing this data to each view of each controller, I was wondering if there is a way to make this data available for every view in the site.

钯.将此数据存储为会话变量/ci 会话 cookie 不是一种选择.

Pd. Storing this data as a session variable / ci session cookie is not an option.

非常感谢.

推荐答案

创建一个 MY_Controller.php 文件并将其保存在 application/core 文件夹中.在其中,类似于:

Create a MY_Controller.php file and save it inside the application/core folder. In it, something like:

class MY_Controller extends CI_Controller {

   public $site_data;

   function __construct() {
       parent::__construct();
       $this->site_data = array('key' => 'value');
   }
}

在您的控制器、视图中,$this->site_data 现在可用.请注意,要使其正常工作,所有其他控制器都需要扩展 MY_Controller 而不是 CI_Controller.

Throughout your controllers, views, $this->site_datais now available. Note that for this to work, all your other controllers need to extend MY_Controllerinstead of CI_Controller.

这篇关于数据可用于 codeigniter 中的所有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆