Welcome Guest, Not a member yet? Register   Sign In
Issue with Session
#1

Hi everyone,

I'm new with CI (4.1.9) and my first steps went well.

But now I'm facing some issue with the session helper.

1.)
I added the following code in my BaseControllers InitController method.
PHP Code:
$this->session = \Config\Services::session(); 

When I use it with $this->session->get() in my controller everything works fine. But I get a
Code:
Call to a member function get() on null

as soon as I use it in one of my models.
How can I use the session service within my models?

2.) In case I check the existence of a session variable I get always a true, even if the variable is not existing.
PHP Code:
$this->session->has('test'); 
Now I'm checking, if this session is null.
Why is the return value of the code above always true?

Thanks in advance.

Could someone support me?
Reply
#2

There's no global object in CI4 like there was in CI3. So if you need to access the session in your model, you need to call the session service again. You can also use the session() helper to minimize the amount of code:
PHP Code:
$this->session session(); 

See https://codeigniter.com/user_guide/libra...-a-session for more details.

For your second question, I don't know why you always get true. All this function do is: 
PHP Code:
return isset($_SESSION[$key]); 
Source: https://github.com/codeigniter4/CodeIgni...n.php#L503
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(04-30-2022, 04:19 AM)THX includebeer Wrote: .
I will try your feedback and come back with a result.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB