CodeIgniter Forums
Global access to $session in controlers and modules - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Global access to $session in controlers and modules (/showthread.php?tid=81205)



Global access to $session in controlers and modules - Acuru - 02-03-2022

According to: https://codeigniter.com/user_guide/libraries/sessions.html

I should be able to initialize i BasicController:
Code:
$session = \Config\Services::session($config);

and then use $session whenever i want, but if i am trying to use it in other controllers without problem, but i am getting: Undefined variable: session

I can initialize:
Code:
$this->session = \Config\Services::session();

and then use in other controllers: $this->session->. It works fine, but i would preffer to use $session-> (for reasons). I need to initialize it somewhere else or something?


RE: Global access to $session in controlers and modules - kenjis - 02-03-2022

It seems you need to learn PHP's variables and properties.

See
https://www.php.net/manual/en/language.variables.basics.php
https://www.php.net/manual/en/language.variables.scope.php
https://www.php.net/manual/en/language.oop5.properties.php
But the explanation in the PHP manual is not easy to understand.


RE: Global access to $session in controlers and modules - InsiteFX - 02-04-2022

Intiallize it in the BaseController then use the session helper $session()->


RE: Global access to $session in controlers and modules - castle - 02-04-2022

(02-03-2022, 05:14 PM)kenjis Wrote: It seems you need to learn PHP's variables and properties.

See
https://www.php.net/manual/en/language.variables.basics.php
https://www.php.net/manual/en/language.variables.scope.php
https://www.php.net/manual/en/language.oop5.properties.php
But the explanation in the PHP manual is not easy to understand.

Sorry, but this is a typical answer that doesn't help anyone.


RE: Global access to $session in controlers and modules - kenjis - 02-04-2022

If a programmer can read and understand the PHP manual, it is the most precise resource.
It helps he/she a lot.