Cakephp 2 - 在另一个组件中使用Auth-Component

时间:2014-03-04 13:56:03

标签: cakephp

我正在尝试使用

访问另一个组件中的AuthComponent
App::uses('AuthComponent', 'Controller/Component');
class AccessComponent extends Object {

public function foo() {
    $this->user = $this->Auth->User();
}

但是检索错误:

  

未定义属性:AccessComponent :: $ Auth   [APP \ Controller \ Component \ AccessComponent.php,第21行]

2 个答案:

答案 0 :(得分:4)

您可以将user用作AuthComponent

static method
App::uses('AuthComponent', 'Controller/Component');
$this->user = AuthComponent::user();

OR包含$components中的组件:

class AccessComponent extends Component {
   public $components = array('Auth');

   public function foo() {
       $this->user = $this->Auth->user();
   }
}

答案 1 :(得分:0)

您可以使用方法访问您的用户

AuthComponent

App::uses('AuthComponent', 'Controller/Component');

$this->user = AuthComponent::user();

CakeSession

App::uses('CakeSession', 'Model/Datasource');

$this->user = CakeSession::read("Auth.User");