2016-11-18 72 views
0

这是我在我的视图代码:会话视图助手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

我连接,但不工作。

+0

你有没有在控制器中使用'Cake \ View \ Helper \ SessionHelper'? –

+0

@MohdSayeed是在我的AppController –

+1

试试这个'$ this-> request-> session() - > read('Auth.User')'在这里你有[更多信息](http://book.cakephp.org /3.0/en/development/sessions.html#accessing-the-session-object) –

回答

0

你可以尝试另一种方式从View

检查Authenticate User从你AppController你可以设置$AuthUser变量

例子:

public function initialize(){ 
    parent::initialize(); 

    $this->loadComponent('Auth', [ 
     #All configurations for AuthComponent 
    ]); 

    $this->set('AuthUser',$this->Auth->user()); #Set for all Views 
} 

Note : Now you can use $AuthUser variable to your View directly.