cakephpcakephp-2.0cakephp-2.6

CakePHP 2.6 Access Controller Object within a Component


I've got a Component that is used by many controllers, and I was wondering if there is a way to access the Controller object from the Component without having to pass it into the Component.

For example I'm currently passing in a reference to the controller into MyAuthComponent:

$this->Auth->isAuthenticated($this);

But, I'd rather be able to invoke this without having to pass the references if possible. Is this possible?


Solution

  • Component callbacks receive controller instance as a parameter. So using your component's startup() callback save the reference to component property and you can then use that in other methods of your component. Check the manual/api for the list of arguments startup() receives.