2016-11-18 2 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/development/sessions.html#accessing-the-session-object) –

答えて

0

あなたのAppControllerあなたが設定することができますから、あなたのView

からAuthenticate Userをチェックするための別の方法を試すことができ$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.

関連する問題