Codeigniter 未加载 CI 超级对象

发布于 2024-11-02 06:22:59 字数 689 浏览 0 评论 0原文

我正在尝试为我的 Codeigniter 应用程序编写一个钩子。

我正在尝试在我的 hook 中捕获一个 session

这是我加载钩子的代码:

$hook['pre_controller'] = array(
  'function' => 'getNav',
  'filename' => 'LoadNav.php',
  'filepath' => 'hooks'
);

这是我试图在钩子中加载的代码:

function getNav()
{
     $CI =& get_instance();
     $level = $CI->session->userdata('level');
}

它不断抛出以下错误:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/LoadNav.php
Line Number: 7

知道我做错了什么吗? get_instance 方法似乎不起作用,对吗?

任何帮助将不胜感激, 谢谢阿兰

I am trying to write a hook for my Codeigniter application.

I'm trying to catch a session in my hook.

Here is my code to load the hook:

$hook['pre_controller'] = array(
  'function' => 'getNav',
  'filename' => 'LoadNav.php',
  'filepath' => 'hooks'
);

And here is the code I'm trying to load in the hook:

function getNav()
{
     $CI =& get_instance();
     $level = $CI->session->userdata('level');
}

It keeps throwing an error which is the following:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/LoadNav.php
Line Number: 7

Any idea of what I'm doing wrong? It seems like the get_instance method is not functioning right?

Any help would be appreciated,
Thanks

Alain

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

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

发布评论

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

评论(1

小霸王臭丫头 2024-11-09 06:22:59

您无法在 pre_controller 挂钩中访问 $CI 实例 - 根据文档:

pre_controller hook 在调用任何控制器之前立即调用。所有基类、路由和安全检查都已完成..

Controller允许访问get_instance()。在实例化控制器之前,无法获取任何内容。

尝试使用 post_controller_constructor 来代替,看看是否能获得所需的结果。

在system/Core/Controller.php中:

class CI_Controller {

// <snip>

    public static function &get_instance()
    {
        return self::$instance;
    }

}
    // END Controller class

You cannot access the $CI instance in a pre_controller hook - as per the docs:

pre_controller hook Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done..

It is the CI Controllerwhich allows access to get_instance(). Until a controller is instantiated, there is nothing to get.

Try post_controller_constructor instead and see if that gets you the desired results.

In system/Core/Controller.php:

class CI_Controller {

// <snip>

    public static function &get_instance()
    {
        return self::$instance;
    }

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