2014-03-04 6 views
0

Я пытаюсь получить доступ к AuthComponent в другом компоненте, используяCakePHP 2 - Использование AuthComponent в другом компоненте

App::uses('AuthComponent', 'Controller/Component'); 
class AccessComponent extends Object { 

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

Но получить ошибку:

Undefined property: AccessComponent::$Auth [APP\Controller\Component\AccessComponent.php, line 21]

+0

Вы всегда следует отметить точное cakep hp, которую вы используете. Также - в общем: помните свой корпус. user()! == User() – mark

ответ

3

Вы можете использовать user как static method от AuthComponent

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

ИЛИ включать компонент в $components:

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

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

добавление 'public $ components = array ('Auth');' к компоненту также будет работать. +1. – AD7six

+1

Да, вы правы. Я обновляю ответ. – cornelb

+0

Спасибо, что решили проблему! –

0

Вы можете использовать methodes для доступа к вашему пользователя

AuthComponent

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

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

CakeSession

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

$this->user = CakeSession::read("Auth.User"); 
Смежные вопросы