cakephp3 在组件中读取会话

cakephp3 read session in component

我有一个为 cakephp3 开发的插件,我想在我的组件中读取存储在会话中的值。

在 cakephp 2 中我使用了:

$userId = CakeSession::read('Auth.User.id');

但是如果我将它用于 cakephp 3 return 我会出现这个错误:

Error: Call to undefined method UserPermissions\Controller\Component\UserPermissionsComponent::_hasSession() 
File /Users/info/Sites/cakephp3/vendor/cakephp/cakephp/src/Network/Session.php 
Line: 382

要在我正在使用的组件中包含会话:

use Cake\Network\Session;

如何读取会话中的值? 谢谢

我会在您的 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')); 
    }        
}