2016-11-18 65 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.