简体   繁体   中英

Use Loader class within a hook in CodeIgniter

I have this function that is in a hook:

public function checkIfLogged() {
    $this->CI = & get_instance();
    if(!$this->CI->session->userdata('logged') ){
        $this->CI->load->view('common/home');
        exit;
    }
}

My problem is that the $this->CI->load->view('common/home'); doesn't actually load the template file at all. Is there any resons why?

I am using a post_controller_constructor hook.

Thanks, Peter

The actual problem was your use of exit . When you load a view, its output is added to the Output class ( system/core/Output.php ). The final view data is then sent (echoed) to the browser by the line $OUT->_display(); found in system/core/CodeIgniter.php .

Since you tossed the exit in there, the script stops, and that display method is never called.

I don't really know what you're trying to do with this hook (it looks like displaying a specified page if a user is logged out or something), but the quickest solution would be to return the output of the view and echo it directly from the hook.

public function checkIfLogged() {
    $this->CI = & get_instance();
    if(!$this->CI->session->userdata('logged') ){
        exit($this->CI->load->view('common/home', null, true));
    }
}

I'd discourage the use of the display_override hook, because your entire controller's code will run before the hook even has a chance to check the particular session data (and it's possible that your controller might even overwrite that session data, giving unexpected results).

You may even consider using a base controller (MY_Controller) instead of a hook. It may be more suitable depending on your situation / desired functionality.

pre_system

Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.

pre_controller

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

post_controller_constructor

Called immediately after your controller is instantiated, but prior to any method calls happening.

post_controller

Called immediately after your controller is fully executed.

display_override

Overrides the _display() function, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output()

cache_override

Enables you to call your own function instead of the _display_cache() function in the output class. This permits you to use your own cache display mechanism.

post_system

Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

so

i think the one you are looking for is display_override

but you can also take a look here Does CodeIgniter have to load view in the final step?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM