0

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

itsme
  • 48,972
  • 96
  • 224
  • 345
Peter Stuart
  • 2,362
  • 7
  • 42
  • 73
  • Dumb question : did you check if session class is loaded and if session userdata doesnt exist ? – Simon May 12 '13 at 23:05
  • Hehe, yeah. Thanks for the comment, but he session is definitely loaded, I sorted that here two minutes ago: http://stackoverflow.com/questions/16512799/why-i-cant-use-sessions-in-a-codeigniter-hook :P – Peter Stuart May 12 '13 at 23:07

2 Answers2

2

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.

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40
  • That's makes perfect sense. Thanks for such a good answer. Very in depth. I just used a redirect so instead of loading that view, it just redirects to a page with a controller that uses that view. Thanks though :) – Peter Stuart May 14 '13 at 19:41
  • That works. I'd recommend looking into a base controller, still, if you're unfamiliar. They're extremely useful. – Aken Roberts May 15 '13 at 02:14
1

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?

Community
  • 1
  • 1
itsme
  • 48,972
  • 96
  • 224
  • 345