【问题标题】:codeigniter hooks can't get $ci object to workcodeigniter 钩子无法让 $ci 对象工作
【发布时间】:2011-12-09 15:14:55
【问题描述】:

我今天刚开始查看钩子,但不是 100% 确定我做错了什么,但是当我尝试在我的函数中使用 $ci 对象时出现错误。

遇到 PHP 错误 严重性:通知 消息:试图获取非对象的属性 文件名:钩子/语言.php 行号:12

我的钩子文件看起来像这样。它在我的应用程序文件夹的 hooks 目录中。

class Language{

    var $ci;

    public function __construct(){
       $this->ci =& get_instance();
    }

    function get_language(){
        echo $this->ci->session->userdata('language');
    }
}

我需要获取会话中的值以在我的函数中使用。我不应该这样做吗?

谢谢!

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    在 Base4/5.php 文件中编写了 get_instance() 函数,它是有条件地加载的,因此在加载之前它不会出现。这就是它给出错误的原因。

    【讨论】:

      【解决方案2】:

      刚刚进行了另一次谷歌搜索,似乎我使用 Pre Controller 的 Hook 是在创建对象之前我已经更改了 Hook,现在它似乎工作正常。

      【讨论】:

        【解决方案3】:

        我使用post_controller_constructor 作为我的钩子,然后 CI 工作了。 而且我必须在配置中打开钩子。

        【讨论】:

          【解决方案4】:

          下面是默认的application/config/hooks.php

              // Stores the requested URL, which will sometimes be different than previous url 
          $hook['post_controller'][] = array(
                                          'class'     => 'App_hooks',
                                          'function'  => 'save_requested',
                                          'filename'  => 'App_hooks.php',
                                          'filepath'  => 'hooks',
                                          'params'    => ''
                                      );
          
          // Allows us to perform good redirects to previous pages.
          $hook['post_controller'][] = array(
                                          'class'     => 'App_hooks',
                                          'function'  => 'prep_redirect',
                                          'filename'  => 'App_hooks.php',
                                          'filepath'  => 'hooks',
                                          'params'    => ''
                                      );
          
          // Maintenance Mode
          $hook['post_controller_constructor'][] = array(
                                          'class'     => 'App_hooks',
                                          'function'  => 'check_site_status',
                                          'filename'  => 'App_hooks.php',
                                          'filepath'  => 'hooks',
                                          'params'    => ''
                                      );
          
          /* End of file hooks.php */
          /* Location: ./application/config/hooks.php */
          

          我已将其更改为下面,它对我来说很好用

           // Stores the requested URL, which will sometimes be different than previous url 
          $hook['post_controller_constructor'][] = array(
                                          'class'     => 'App_hooks',
                                          'function'  => 'save_requested',
                                          'filename'  => 'App_hooks.php',
                                          'filepath'  => 'hooks',
                                          'params'    => ''
                                      );
          
          // Allows us to perform good redirects to previous pages.
          $hook['post_controller'][] = array(
                                          'class'     => 'App_hooks',
                                          'function'  => 'prep_redirect',
                                          'filename'  => 'App_hooks.php',
                                          'filepath'  => 'hooks',
                                          'params'    => ''
                                      );
          
          // Maintenance Mode
          $hook['post_controller'][] = array(
                                          'class'     => 'App_hooks',
                                          'function'  => 'check_site_status',
                                          'filename'  => 'App_hooks.php',
                                          'filepath'  => 'hooks',
                                          'params'    => ''
                                      );
          
          /* End of file hooks.php */
          /* Location: ./application/config/hooks.php */
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-08-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-30
            • 1970-01-01
            • 2015-04-16
            • 1970-01-01
            相关资源
            最近更新 更多