php CodeIgniter 全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2527119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:50:36  来源:igfitidea点击:

CodeIgniter global variable

phpcodeigniter

提问by Shishant

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

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

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

我还按照本指南扩展核心控制器自动加载了一个控制器

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

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

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

目前我必须为每个手动编写 $data['todo'] 并从自动加载的模型中获取信息。

Thank You.

谢谢你。

回答by Shishant

1: Create MY_Controllerin application/librarieswith following:

1:创建MY_Controllerapplication/libraries与下列内容:

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

2: Replace Controllerto MY_Controllerin all your controller files and load views with $this->data

2:替换ControllerMY_Controller所有控制器文件并加载视图$this->data

class Contact extends Controller { //to.. } 

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

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

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

通过这种方式,您可以在MY_Controller加载设置中执行适用于整个站点的默认功能。

回答by stormdrain

I ran into a similar problem earlier today. I found that an easier way, rather than globals, was to use constants. You can define a constants file that will load from your index.php file:

我今天早些时候遇到了类似的问题。我发现比全局变量更简单的方法是使用常量。您可以定义一个将从您的 index.php 文件加载的常量文件:

// Include additional constants
$defines_file = 'includes/defines.php';
if (file_exists($defines_file))
{
    require_once($defines_file);
} 

Then you can add your constants to the defines.phpfile:

然后您可以将常量添加到defines.php文件中:

define(MY_CONSTANT,'my constant info');

This way they will be available in any file throughout the system either directly: echo MY_CONSTANT;or you can assign them to variables.

这样,它们将在整个系统的任何文件中直接可用:echo MY_CONSTANT;或者您可以将它们分配给变量。

I decided this way would be easier for me as I would only have 1 location to go to when/if I needed to change the constants.

我决定这种方式对我来说更容易,因为当/如果我需要更改常量时,我​​只有 1 个位置可以去。

More: http://codeigniter.com/forums/viewthread/56981/#280205

更多:http: //codeigniter.com/forums/viewthread/56981/#280205

回答by pkdkk

I used a helper function to call a global function!

我用了一个辅助函数来调用一个全局函数!

eg.

例如。

function get_user($userid){
    $CI =& get_instance();

    $query = $CI->db->get_where('users', array('id' => $userid), 1, 0);
    foreach ($query->result() as $row){
        // Return a object with userdata!
        return $row;
    }
}

Now I have access to my userdata everywhere..

现在我可以在任何地方访问我的用户数据..

回答by Stephen Curran

Rather than making the view data global, I'd recommend using HMVCto build a module to produce this sidebar view. HMVC is a nice clean way of coding partial views.

我建议使用HMVC构建一个模块来生成此侧边栏视图,而不是将视图数据设为全局。HMVC 是编码部分视图的一种非常干净的方式。