codeigniter中所有视图的可用数据 [英] Data available for all views in codeigniter
问题描述
我有一个变量,包含整个网站中应该存在的数据。而不是将这些数据传递到每个控制器的每个视图,我想知道是否有一个方法,使这个数据可用于网站中的每个视图。
Pd。将此数据存储为会话变量/ ci会话cookie不是一个选项。
非常感谢。
创建一个 MY_Controller.php
文件,并将其保存在application / core文件夹中。在它,类似:
class MY_Controller扩展CI_Controller {
public $ site_data;
function __construct(){
parent :: __ construct();
$ this-> site_data = array('key'=>'value');
}
}
在您的控制器中, > $ this-> site_data 现在可用。
注意,为了这个工作,所有其他控制器需要扩展 MY_Controller
,而不是 CI_Controller
。 / p>
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.
Pd. Storing this data as a session variable / ci session cookie is not an option.
Thanks so much.
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');
}
}
Throughout your controllers, views, $this->site_data
is now available.
Note that for this to work, all your other controllers need to extend MY_Controller
instead of CI_Controller
.
这篇关于codeigniter中所有视图的可用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!