CodeIgniter Forums

Full Version: Cannot Load Session/Model in Hook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jitterbug[/eluser]
I am trying to use session information in a hook. Here is the hook

Code:
class Template extends Controller {
      function Template() {
          parent::Controller();
          $this->load->helper('url');
          $this->load->helper('form');
          $this->load->model('userauth');
          $this->load->library('session');
      }

However similar code works fine in the default controller

Code:
class DefaultController extends Controller{

    function DefaultController (){
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model('userauth');
        $this->load->scaffolding('users');
    }}

This is the error I am getting

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Template::$session

Filename: hooks/template.php

Line Number: 17

So loading the session in a model when it was already autoloaded was a bad idea... However, it is still not recognizing session within the Template hook class.

El Forum

[eluser]jitterbug[/eluser]
Another case of know what youre using before you use it!

$this->load->session is the correct way to grab the session. Reloading the library will cause conflicts in your code...

As an aside it took me 3 hours to figure this out (and only because I used print_r($this) to sort through the tree until session. It would be nice if the autoload page told new users how to load in classes that have been autoloaded. Perhaps its there somewhere, but I really poured over the wiki, user guide, and forums to find an answer to this.

Moreover it appears many users are incorrectly using $this->load->library['session'] to load the session into their controller object when in reality they just want $this->load->session. While this is obvious in retrospect I dont think it is obvious to new users who do not understand things.

Thanks and yay!