CodeIgniter 中的全局变量不起作用

发布于 2024-12-05 11:31:17 字数 864 浏览 0 评论 0原文

我想通过创建自己的库和配置文件在 CodeIgniter 中生成全局变量。这是我在库文件中编写的内容,比如说 globalvars.php。我把它放在/application/libraries 中。

class Globalvars{

    function __construct($config = array()) 
    {
        foreach ($config as $key => $value) {
            $data[$key] = $value;
        }
        $CI =& get_instance();
        $CI->load->library('session');
        $CI->load->vars($data); 
    }
}

我希望存储在会话中的用户 ID 在全局变量中可用,因此我将其写入配置文件中。它也被命名为globalvars.php。它位于 /application/config 目录中。

$config['user']=$this->session->userdata('id');

然后我通过以这种方式将其写入我的控制器来测试它是否正常工作。

echo $data['user'];

但我在浏览器中收到此错误

Message: Undefined property: CI_Loader::$session
Filename: config/globalvars.php

似乎会话功能尚未定义。我怎样才能让它发挥作用?我在这里错过了什么?任何帮助将不胜感激。

I want to generate global variables in CodeIgniter by creating my own library and config file. This is what I wrote ini my library file, let's say globalvars.php. I put it in /application/libraries.

class Globalvars{

    function __construct($config = array()) 
    {
        foreach ($config as $key => $value) {
            $data[$key] = $value;
        }
        $CI =& get_instance();
        $CI->load->library('session');
        $CI->load->vars($data); 
    }
}

I want the user id stored in the session to be available in global variable, so I wrote this in my config file. It's named also globalvars.php. It's in /application/config directory.

$config['user']=$this->session->userdata('id');

I then test to see if it's working by write it in my controller this way.

echo $data['user'];

But I get this error in the browser

Message: Undefined property: CI_Loader::$session
Filename: config/globalvars.php

It seems that the session functions is not defined yet. How can I get it work? What have I missed here? Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2024-12-12 11:31:17

您不能在 config 文件中使用 session 库。

config 文件在任何库之前加载,因此 $this->session 未定义。

You cannot use the session library in config file.

The config files are loaded before any libraries, so $this->session is undefined.

哭泣的笑容 2024-12-12 11:31:17

必须加载 config.php 才能初始化 Session 类,因为它从该文件读取设置。

此类事情的许多问题(设置一些“全局”数据)可以使用基本控制器来解决,并在控制器中扩展它。

// core/MY_Controller.php
MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct(); // Now the Session class should be loaded
        // set config items here 
    }
}

“普通”控制器现在将扩展 MY_Controller 来利用这一点。

请参阅:http://codeigniter.com/user_guide/general/core_classes.html 了解更多信息细节。

此外,当您 load->vars() 时,它们仅可用于视图层,它不会创建一个名为 $data 的全局变量,正如您所看到的那样正在尝试访问。如果这样做:

$this->load->vars(array('user' => '1'));

您将在 $this->load->view() 加载的文件中访问它,如下所示:

echo $user;  // outputs "1"

请参阅:http://codeigniter.com/user_guide/libraries/loader.html

$this->load->vars($array)

该函数采用关联数组作为输入并生成
使用 PHP 提取函数的变量。该函数产生
与使用 $this->load->view() 的第二个参数的结果相同
上面的函数。您可能想使用此功能的原因
独立地是如果你想在中设置一些全局变量
控制器的构造函数,并让它们在任何
查看文件从任何函数加载。您可以多次致电
这个功能。数据被缓存并合并到一个数组中
转换为变量。

我想说的是,作为一名经验丰富的 Codeigniter 用户,“全局变量”类的概念有点奇怪,而且可能没有必要,特别是当它已经很容易获取和设置配置项时。您肯定会遇到一些令人困惑的问题以及与此方法的变量名称冲突(在每个请求上预加载大量视图变量)。

The config.php has to be loaded in order for the Session class to even be initialized, as it reads settings from that file.

A lot of issues with this type of thing (setting some "global" data) can be resolved using a base controller, and extending it in your controllers.

// core/MY_Controller.php
MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct(); // Now the Session class should be loaded
        // set config items here 
    }
}

"Normal" controllers will now extend MY_Controller to take advantage of this.

See: http://codeigniter.com/user_guide/general/core_classes.html for more details.

In addition, when you load->vars(), they are available to the view layer only, it does not create a global variable called $data as you seem to be trying to access. If you do this:

$this->load->vars(array('user' => '1'));

You would access it in a file loaded by $this->load->view() like this:

echo $user;  // outputs "1"

See: http://codeigniter.com/user_guide/libraries/loader.html

$this->load->vars($array)

This function takes an associative array as input and generates
variables using the PHP extract function. This function produces the
same result as using the second parameter of the $this->load->view()
function above. The reason you might want to use this function
independently is if you would like to set some global variables in the
constructor of your controller and have them become available in any
view file loaded from any function. You can have multiple calls to
this function. The data get cached and merged into one array for
conversion to variables.

I will say that as an experienced Codeigniter user, the concept of a "global vars" class is a bit wonky and probably unnecessary, especially when it's already so easy to get and set config items. You could definitely run into some confusing issues and variable name conflicts with this method (pre-loading lots of view variables on every request).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文