在使用destroy函数时,在不在对象上下文中时使用$ this

时间:2016-10-19 10:14:18

标签: session cakephp cakephp-3.0

这是我在同一页面的控制器中的登录和注销功能。当我在注销时使用Session :: destroy()时,它会抛出一个错误"当不在对象上下文中时使用$ this"。并告诉我如何检查会话是否有效。 提前谢谢

public function login(){
       $session = $this->request->session();  
      $student12 = TableRegistry::get('users');    
         $email=$this->request->data('email');
         $password=$this->request->data('password');
          $query12 = $student12->find();
          $query12->where(['email'=>$email]);
        foreach($query12 as  $datax)
        {
        if($datax['password']===$password&&$datax['email']===$email)
        {   

//Session::write($key, $value);
//Session::read($key);
         $this->redirect(['controller'=>'Panal','action' => 'Home']);
        }else{
          $this->redirect(['controller'=>'Student','action' => 'index']);  
        }
    }
    }

    public function logout(){

      Session::destroy();
       $this->redirect(['controller'=>'Panal','action' => 'Home']);

    }

1 个答案:

答案 0 :(得分:0)

假设您想使用与CakePHP 3默认的Users模型不同的模型,您可以将其放在AuthComponent的设置中。

// AppController.php -> initialize method
$this->Auth->config('authenticate', [
    'Form' => ['userModel' => 'Members']
]);

RTFM ref:http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers