问题
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');
回答1:
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');
回答2:
I use this its works fine
$session = $this->request->session();
$session->write('annul_income','$100,00,00');//Write
echo $session->read('annul_income')//To read the session value o/p:$100,00,00
回答3:
Prior to 3.6.0 use getRequest()
and getSession()
instead.
$name = $this->getRequest()->getSession()->read('User.name');
And if you are accessing the session multiple times, you will probably want a local variable.
$session = $this->getRequest()->getSession();
$name = $session->read('User.name');
来源:https://stackoverflow.com/questions/30568653/how-to-read-and-write-session-in-cakephp-3-0