CodeIgniter 中的全局变量不起作用
我想通过创建自己的库和配置文件在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在
config
文件中使用session
库。config
文件在任何库之前加载,因此$this->session
未定义。You cannot use the
session
library inconfig
file.The
config
files are loaded before any libraries, so$this->session
is undefined.必须加载 config.php 才能初始化 Session 类,因为它从该文件读取设置。
此类事情的许多问题(设置一些“全局”数据)可以使用基本控制器来解决,并在控制器中扩展它。
“普通”控制器现在将扩展 MY_Controller 来利用这一点。
请参阅:http://codeigniter.com/user_guide/general/core_classes.html 了解更多信息细节。
此外,当您
load->vars()
时,它们仅可用于视图层,它不会创建一个名为$data
的全局变量,正如您所看到的那样正在尝试访问。如果这样做:您将在
$this->load->view()
加载的文件中访问它,如下所示:请参阅:http://codeigniter.com/user_guide/libraries/loader.html
我想说的是,作为一名经验丰富的 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.
"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:You would access it in a file loaded by
$this->load->view()
like this:See: http://codeigniter.com/user_guide/libraries/loader.html
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).