会话视图helper CakePHP3



这是我的代码在我的视图:

if($this->request->session->read('Auth.User')) {
    echo $this->Html->link('Log Out', array('controller' => 'users','action' => 'logout'));
}
if(!$this->request->session->read('Auth.User')) {
    echo $this->Html->link('Log Out', array('controller' => 'users','action' => 'logout'));
}

但是我得到这个错误:

Call to a member function read() on null

您可以尝试另一种方法来检查Authenticate User从您的View

从你的AppController你可以设置$AuthUser变量

例子:

public function initialize(){
    parent::initialize();
    $this->loadComponent('Auth', [
       #All configurations for AuthComponent
    ]);
    $this->set('AuthUser',$this->Auth->user()); #Set for all Views
}

注意:现在你可以直接使用$AuthUser变量到你的View

最新更新