How to read and write Session in Cakephp 3.0

不打扰是莪最后的温柔 提交于 2019-12-09 04:45:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!