Welcome Guest, Not a member yet? Register   Sign In
Using sessions in library or helper
#1

[eluser]N.S.G.[/eluser]
Hi there,

I'm developing an application and right now I'm writing a message class to store messages in ("Your post was succesfully updated" and such) and have them shown to the user.

At first I used a library with a messages[] array, which worked fine as long as I stayed in the same controller but now the situation occurs that I have to switch to another controller using redirect(). So for example: updating a post in the "post" controller, and then redirecting to the "user" controller to show the user dashboard (where I want to display "post updated succesfully".

Now I figured I'd just put the messages array in a session, problem solved, but that doesn't seem to work because I can't call sessions from a library (nor from a helper).

How should I solve this? (I figure I can copy my set_message() and get_messages() functions in every controller but that'd be inconvenient).

! I just answered my own question: I could put these functions in the basic controller and have all other controllers extend from that one. Still: do you think that would be the best solution?

Thanks!
#2

[eluser]Rob Gordijn[/eluser]
yes, that is a good solution, search for MY_Controller, you will find enough examples.

by the way, hint: lookup the manual for flash messages
#3

[eluser]Zack Kitzmiller[/eluser]
You can call sessions from a library, or helper, or any where else that you want, you just need to use the CodeIgniter SuperObject.

$CI =& get_instance();
echo $CI->session->userdata('message');

Etc.
#4

[eluser]N.S.G.[/eluser]
Thanks both! I already used Rob's solution, which works fine for me but it's good to know about the superobject.




Theme © iAndrew 2016 - Forum software by © MyBB