CodeIgniter Forums
What is the recommended method of having sessions always on - 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: What is the recommended method of having sessions always on (/showthread.php?tid=80284)



What is the recommended method of having sessions always on - paulkd - 10-12-2021

Hi All,

As per the title, I've been looking at filters and events. In CI3 I just autoloaded sessions.

I want sessions up and running as soon as possible to make decisions on their values.


RE: What is the recommended method of having sessions always on - manager - 10-12-2021

Hi. You can initialize it in a BaseController and other controllers will extend it.
PHP Code:
....
$protected $session;
...

public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
{
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        $this->session = \Config\Services::session();
        ....
}
... 

Than you can access it like $this->session from ALL controllers extended from Base.