Thursday, March 10, 2022

[FIXED] How to read and write Session in Cakephp 3.0

Issue

I am new to cake 3.0. I have read documentation on http://book.cakephp.org/3.0/en/development/sessions.html But I am not able to write sessions.

use Cake\Network\Session\DatabaseSession;

$session->write('Config.language', 'eng'); 
$session->read('Config.language');

Solution

Update : For CakePHP 3.6+, see @shubham715 answer

The following applies to CakePHP before 3.6 :

You need to set $session :

$session = $this->request->session();
$session->write('Config.language', 'eng'); 
$session->read('Config.language');

And then you'll be able to read and write in your session

Or you can direclty read and write :

$this->request->session()->write('Config.language', 'eng');
$this->request->session()->read('Config.language');


Answered By - Jun

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.