Codeigniter 未加载 CI 超级对象
我正在尝试为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在
pre_controller
挂钩中访问$CI
实例 - 根据文档:Controller
允许访问get_instance()
。在实例化控制器之前,无法获取任何内容。尝试使用
post_controller_constructor
来代替,看看是否能获得所需的结果。在system/Core/Controller.php中:
You cannot access the
$CI
instance in apre_controller
hook - as per the docs:It is the CI
Controller
which allows access toget_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
: