codeigniter hooks can't get $ci object to work

回眸只為那壹抹淺笑 提交于 2019-12-24 09:27:29

问题


I've just started looking at hooks today not 100% sure what I'm doing wrong but I'm getting an error when I try and use the $ci object in my function.

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

My hooks file looks like this. It's in the hooks directory in my application folder.

class Language{

    var $ci;

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

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

I need to get the value in the session to use in my function. Am I not supposed to do it like this?

Thanks you!


回答1:


In the Base4/5.php file the get_instance() function is written and it is conditionally loaded so it won’t be present until after it is loaded. And that's the reason its giving error.




回答2:


Just done another Google search and it seems the Hook I was using Pre Controller was before the object was created I've changed the hook and it seems to work fine now.




回答3:


I used post_controller_constructor for my hook, then the CI worked. And I had to turn on hooks in the config.




回答4:


Below is default 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 */

I have changed it to below and it works fine for me

 // 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 */


来源:https://stackoverflow.com/questions/8447565/codeigniter-hooks-cant-get-ci-object-to-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!