CodeIgniter Forums
How to start session globally? - 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: How to start session globally? (/showthread.php?tid=71769)

Pages: 1 2 3


How to start session globally? - happyape - 09-21-2018

I looked at the documentation, https://bcit-ci.github.io/CodeIgniter4/libraries/sessions.html?#initializing-a-session, which says,

Code:
To access and initialize the session:

$session = \Config\Services::session($config);

Am I supposed to put this code in the constructor of every Controller class where I need to manipulate session data?

I found that I can also use start session using this helper
Code:
session()->start();
but again I need to declare that in all files before I can use session.

I need to know the same for db and custom common functions helper file.

$db = \Config\Database::connect();

All I am trying to find out if I should be placing these in a common file? Or perhaps, declaring in each file is the suggested practice???


RE: How to start session globally? - sv3tli0 - 09-21-2018

Just make 1 Controller that will extend CodeIngiter Controller and it will be extended by your all other controllers (core extending in docs).
There in the constructor of this App\Controller you can pre-assign all services that you want to be always available as protected variables.
Code:
$this->session = \Config\Services::session();


At models you have the default database as $this->db available.. Further there is an example how you are able to pick another db connection for a model in case you have multiple DB's with just setting correct DB group..
Code:
protected $DBGroup = 'group_name';"



RE: How to start session globally? - happyape - 09-21-2018

Yes I understand and I was going to add that approach in my question too... but again, I thought there might be a another simpler solution for this specific requirement than to do class extensions etc.


RE: How to start session globally? - dave friend - 09-21-2018

Perhaps Events could be used?


RE: How to start session globally? - happyape - 09-21-2018

(09-21-2018, 05:29 AM)dave friend Wrote: Perhaps Events could be used?

Are you suggesting some thing like this below?

// Use a Closure
Events::on('pre_system', function(...$params)
{
      $session = \Config\Services:Confusedession();
});

Would that make $session variable globally available?


RE: How to start session globally? - sv3tli0 - 09-21-2018

"global variable" is not in OOP style.. Usually when you want something you load it somehow.
This will only guarantee that session is started ..
From that point to set/get things in the session is up to you. $_SESSION is available to get it and in the few places where you will set something you can simple get the same
Code:
$session = \Config\Services::Session();



RE: How to start session globally? - happyape - 09-21-2018

(09-21-2018, 06:10 AM)sv3tli0 Wrote: "global variable" is not in OOP style.. Usually when you want something you load it somehow.
This will only guarantee that session is started ..
From that point to set/get things in the session is up to you. $_SESSION is available to get it and in the few places where you will set something you can simple get the same
Code:
$session = \Config\Services::Session();

Yes I understand and that's now what I want (to make that available global)
The aim is
To start session for all pages
To connect to database - required for all pages

At the moment, I can do that above fine by creating a base controller and extending to it.


RE: How to start session globally? - kilishan - 09-21-2018

My suggestion: don't worry about loading the session globally. First - that means that if the session isn't needed it isn't loaded which can be good for performance and some security things, IIRC. Second - whenever you first call the service, or use the the service() helper method, the session will be automatically initialized and ready for use.

Code:
// These all ensure session is initialized:
$session = \Config\Services::Session();
session('key');
session()->get('key');
session()->set('key', $value);



RE: How to start session globally? - happyape - 09-21-2018

(09-21-2018, 06:16 AM)kilishan Wrote: My suggestion: don't worry about loading the session globally. First - that means that if the session isn't needed it isn't loaded which can be good for performance and some security things, IIRC. Second - whenever you first call the service, or use the the service() helper method, the session will be automatically initialized and ready for use.

Code:
// These all ensure session is initialized:
$session = \Config\Services::Session();
session('key');
session()->get('key');
session()->set('key', $value);

Thank you for your reply.
A common requirement is check if user is logged in or not. That check is on most of the pages to do even simplest task e.g. to show login or logout button. That is what triggered to learn about the best practices.


RE: How to start session globally? - happyape - 09-21-2018

(09-21-2018, 06:16 AM)kilishan Wrote: My suggestion: don't worry about loading the session globally. First - that means that if the session isn't needed it isn't loaded which can be good for performance and some security things, IIRC. Second - whenever you first call the service, or use the the service() helper method, the session will be automatically initialized and ready for use.

Code:
// These all ensure session is initialized:
$session = \Config\Services::Session();
session('key');
session()->get('key');
session()->set('key', $value);

Hi Kilishan. What do you advise for running a db query inside a function in a helper file?
I created a custom helper file and put it in the Helpers/mycustom_helper.php
In this file, I want to create a function say,
Code:
function get_some_stats() {
/// I suppose I can do this -> $db = \Config\Database::connect();
// but if there are 20 different functions, then I'd have to do that in all function repeatedly
}

Am I correct? Just clarifying.