我有一个为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若要在组件中包含会话,请使用:
use Cake\Network\Session;如何在会话中读取值?谢谢
发布于 2015-04-09 19:13:39
我会在您的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'));
}
}https://stackoverflow.com/questions/29546651
复制相似问题