• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to start session globally?

#1
I looked at the documentation, https://bcit-ci.github.io/CodeIgniter4/l...-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???
Reply

#2
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';"
Best VPS Hosting : Digital Ocean
Reply

#3
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.
Reply

#4
Perhaps Events could be used?
Reply

#5
(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?
Reply

#6
"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();
Best VPS Hosting : Digital Ocean
Reply

#7
(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.
Reply

#8
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);
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply

#9
(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.
Reply

#10
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.