Can not load session into hooks |
[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( hooks/template.php Code: class Template extends Controller 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 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!
[eluser]skunkbad[/eluser]
It would be best to do this by extending the Controller class.
[eluser]bryantighe[/eluser]
Can you be more specific? Someone else said that it would be best to use hooks.
[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.
[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?
[eluser]skunkbad[/eluser]
What if you do this: Code: class Template
[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.
[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( hooks/template.php Code: class Template |
Welcome Guest, Not a member yet? Register Sign In |