在 cakephp 2 中的行为中读取会话变量 [英] Reading a session variable inside a behavior in cakephp 2
问题描述
我有一个行为可以根据存储在会话中的用户 ID 来隔离用户数据.在 CakePHP 1.3 中,你可以这样做:
I have a behavior which enables segregation of user data based on the user id stored in the session. In CakePHP 1.3 you could do this:
App::import('Component', 'Session');
$session = new SessionComponent();
$session->read('Auth.User.id');
但是在 CakePHP 2 中,您不能在行为中实例化这样的组件,因为 Component __construct 需要控制器的 ComponentCollection 作为参数.
But in CakePHP 2, you can't instantiate a component like that in a behavior because the Component __construct requires the Controller's ComponentCollection as a parameter.
是否可以在 CakePHP 2 的行为中访问会话变量?最好的方法是什么?
Is it possible to access a session variable inside a behavior in CakePHP 2? What's the best way to do it?
推荐答案
如果您查看 SessionComponent
代码,您会发现它只是 CakeSession
的包装器> 班级.
If you look at the SessionComponent
code, you will see that it is only a wrapper for the CakeSession
class.
因此您可以执行以下操作:
So you can do the following:
App::uses('CakeSession', 'Model/Datasource');
$user_id = CakeSession::read('Auth.User.id');
这篇关于在 cakephp 2 中的行为中读取会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!