How to start session globally? |
I looked at the documentation, https://bcit-ci.github.io/CodeIgniter4/l...-a-session, which says,
Code: To access and initialize the session: 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(); 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???
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
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.
(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:ession(); }); Would that make $session variable globally available?
"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
(09-21-2018, 06:10 AM)sv3tli0 Wrote: "global variable" is not in OOP style.. Usually when you want something you load it somehow. 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.
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: (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. 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. (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. 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() { Am I correct? Just clarifying. |
Welcome Guest, Not a member yet? Register Sign In |