Welcome Guest, Not a member yet? Register   Sign In
Can not load session into hooks
#1

[eluser]bryantighe[/eluser]
I'm using a hook to load a header template to the top of every page. In my hook class, I want to load the session so that I can test if the user is logged and and do some other things as well. However, I can't seem to load the session, I keep getting an error.

Here's the code:

config/hooks.php (and yes, enable hooks is set to true)
Code:
$hook['post_controller_constructor'] = array(
                      'class'  => 'Template',
                      'function' => 'header',
                      'filename' => 'template.php',
                      'filepath' => 'hooks'
                      );

hooks/template.php
Code:
class Template extends Controller
{

      function Template() {
          parent::Controller();
          
           $this->CI=&get;_instance();
       if (!isset($this->CI->session)) {
               $this->CI->load->library('session');
            }
        
      }

      function header() {
          $data = array();
          if( $this->CI->session->userdata('logged_in'))
          {
            $data['logged_in'] = true;
          }
          else
          {
            $data['logged_in'] = false;
          }
          
          $this->load->view('header', $data);
      }
      
      function footer() {
          $this->load->view('footer');
      }
}

I've tried infinite permutations of the above code, loading the CI variable with the get_instance() function, but I keep getting the following errors:

Quote:A PHP Error was encountered

Severity: 4096

Message: Object of class __PHP_Incomplete_Class could not be converted to string

Filename: libraries/Session.php

Line Number: 715
A PHP Error was encountered

Severity: Notice

Message: Object of class __PHP_Incomplete_Class to string conversion

Filename: libraries/Session.php

Line Number: 715
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Template::$session

Filename: hooks/template.php

Line Number: 18
Fatal error: Call to a member function userdata() on a non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\steve\system\application\hooks\template.php on line 18

Line 18 is the line that says
Code:
if( $this->session->userdata('logged_in'))


I've followed several examples that others have posted on the forums here, but none of them seem to work for me, or the questions are not fully answered. Can someone give me some clean code that actually works?

Thanks!
#2

[eluser]skunkbad[/eluser]
It would be best to do this by extending the Controller class.
#3

[eluser]bryantighe[/eluser]
Can you be more specific? Someone else said that it would be best to use hooks.
#4

[eluser]skunkbad[/eluser]
[quote author="bryantighe" date="1275622051"]Can you be more specific? Someone else said that it would be best to use hooks.[/quote]

I may be wrong, but it is my understanding that hooks simply provide a way for you to call functions at certain times. The functions are not within a class as methods, but just plain php functions. You have yours in a class, and I have not seen this type of usage before.

If you want to see an example of MY_Controller in action, take a look at Community Cart:

http://bitbucket.org/skunkbad/community-cart

I use MY_Controller primarily for the authentication of users. I also have some hooks in Community Cart, and you can see how they are being used.
#5

[eluser]bryantighe[/eluser]
While I'm new to codeigniter, I've used many other MVC frameworks in the past. They all have pre_ and post_ controller operations similar to hooks that are used for this very purpose (creating headers/footers, user session management, etc.).

Can someone tell me how to get the session in a hook?
#6

[eluser]skunkbad[/eluser]
What if you do this:

Code:
class Template
{
    function header(){
        $local_CI =& get_instance();
        if (!isset($local_CI->session))
        {
            $local_CI->load->library('session');
        }
        $data = array();
        if( $local_CI->session->userdata('logged_in'))
        {
            $data['logged_in'] = true;
        }
        else
        {
            $data['logged_in'] = false;
        }
        $local_CI->load->view('header', $data);
    }
}
#7

[eluser]danmontgomery[/eluser]
Your hook shouldn't be extending the controller class.

IIRC, the "Object of class __PHP_Incomplete_Class" error pops up when you try to reference an object in the session before instantiating that class... Don't know what line 715 of session.php is, it's not the default session library (native session library, maybe?), whatever's on that line might give you/us a clue.
#8

[eluser]bryantighe[/eluser]
Thanks skunkbad and noctrum, the code you posted worked like a charm. My problems were extending from the controller class (which I should have known not to do!) and trying to use $this->CI instead of just a local variable.

Now I can use hooks to deal with the user sessions and load a header and footer on every page.

For the record, here's my final working code:

config/hooks.php
Code:
$hook['post_controller_constructor'] = array(
                      'class'  => 'Template',
                      'function' => 'header',
                      'filename' => 'template.php',
                      'filepath' => 'hooks'
                      );

                      
$hook['post_controller'] = array(
                      'class'  => 'Template',
                      'function' => 'footer',
                      'filename' => 'template.php',
                      'filepath' => 'hooks'
                      );

hooks/template.php
Code:
class Template
{
    function header(){
        $local_CI =& get_instance();
        if (!isset($local_CI->session))
        {
            $local_CI->load->library('session');
        }
        $data = array();
        if( $local_CI->session->userdata('logged_in'))
        {
            $data['logged_in'] = true;
        }
        else
        {
            $data['logged_in'] = false;
        }
        $local_CI->load->view('header', $data);
    }
    
    function footer(){
        $local_CI =& get_instance();
        if (!isset($local_CI->session))
        {
            $local_CI->load->library('session');
        }
        $data = array();
        if( $local_CI->session->userdata('logged_in'))
        {
            $data['logged_in'] = true;
        }
        else
        {
            $data['logged_in'] = false;
        }
        $local_CI->load->view('footer', $data);
    }
    
}




Theme © iAndrew 2016 - Forum software by © MyBB