CodeIgniter全局变量 [英] CodeIgniter global variable

查看:82
本文介绍了CodeIgniter全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在所有视图中使用 $ data $ this-> load-> view('my_view',$ data);

I am using $data in all my views $this->load->view('my_view', $data);

我也按照本指南自动加载控制器扩展核心控制器

I have also autoload a Controller following this guide Extending Core Controller

但我想让$ data全局,因为在 views 有一个边栏,它是整个项目的常量,并显示通过自动加载控制器中的db获取的信息

But I want to make $data global because in views there is a sidebar which is constant for whole project and displays info fetched through db in autoloaded controller

目前我必须手动写$ data ['

Currently I have to manually write $data['todo'] for each and fetch info from autoloaded model.

谢谢。

推荐答案

1:使用以下命令在应用程序/ libraries 中创建 MY_Controller / p>

1: Create MY_Controller in application/libraries with following:


class MY_Controller extends Controller {  
  var $data;      
  //constructor function              
}


2:将所有控制器文件中的控制器更改为 MY_Controller ,并使用 $ this- > data

2: Replace Controller to MY_Controller in all your controller files and load views with $this->data


class Contact extends Controller { //to.. } 

class Contact extends MY_Controller { 
 $this->load->view('contact_view', $this->data);
}


适用于整个网站 MY_Controller 喜欢加载设置。

this way you can perform default functions that are applicable for whole site in MY_Controller like loading settings.

这篇关于CodeIgniter全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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