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');
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.
Is it possible to access a session variable inside a behavior in CakePHP 2? What's the best way to do it?
In CakePHP 2.0 you can also simply call the Session-methods via the static CakeSession::method() without having to load anything... ;-)
If you look at the
SessionComponent
code, you will see that it is only a wrapper for theCakeSession
class.So you can do the following: