我有一个为cakephp3开发的插件,在我的组件里面我想读取存储在会话中的值。
在cakephp 2中,我用过:
$userId = CakeSession::read('Auth.User.id');
但如果我将它用于cakephp 3,请将此错误告诉我:
Error: Call to undefined method UserPermissions\Controller\Component\UserPermissionsComponent::_hasSession()
File /Users/info/Sites/cakephp3/vendor/cakephp/cakephp/src/Network/Session.php
Line: 382
使用以下内容在组件I中包含会话:
use Cake\Network\Session;
如何在会话中读取值? 感谢
答案 0 :(得分:4)
我会在你的initialize()
方法中实现这一点:
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
class YourComponent extends Component{
public $controller = null;
public $session = null;
public function initialize(array $config)
{
parent::initialize($config);
// ....
/**
* Get current controller
*/
$this->controller = $this->_registry->getController();
$this->session = $this->controller->request->session();
// You can then use $this->session in any other methods
// If debug = true else use print_r() to test
debug($this->session->read('Auth.User.id'));
}
}