2017-06-06 95 views
1

我想從另一個模型登錄,我有一個錯誤。cakephp 3用於登錄另一個模型比用戶身份驗證

這是我的代碼學生模型

var $name= 'Student'; 

public $components = array(
    'Session', 
    'Auth' => array(
     'loginAction' => array(
      'controller' => 'students', 
      'action' => 'login', 
      'plugin' => 'students' 
     ), 
     'authError' => 'Did you really think you are allowed to see that?', 
     'authenticate' => array(
      'Form' => array(
       'fields' => array(
        'username' => 'username', //Default is 'username' in the userModel 
        'password' => 'password' //Default is 'password' in the userModel 
       ) 
      ) 
     ) 
    ) 
); 

StudentsController看起來像

public function beforeFilter(Event $event) { 
    parent::beforeFilter($event); 
    $this->Auth->allow('login'); 
} 


public function login() { 

    if($this->Session->check('Auth.Student')){ 
     $this->redirect(array('action' => 'login')); 
    } 


    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      $this->Session->setFlash(__('Welcome, '. $this->Auth->student('username'))); 
      $this->redirect($this->Auth->redirectUrl()); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password')); 
     } 
    } 
} 

而且AppController的是

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

    $this->loadComponent('RequestHandler'); 
    $this->loadComponent('Flash'); 
} 

    public $components = array(
    'Session', 
    'Auth' => array(
     'loginRedirect' => array('controller' => 'students', 'action' => 'login'), 
     'loginAction' => array('controller' => 'students', 'action' => 'login'), 



    )); 

public function beforeFilter(Event $event) { 

    $this->Auth->authenticate=array (
     'loginAction' => [ 
      'controller' => 'Students', 
      'action' => 'login', 
      'plugin' => false, 
     ], 
     'Basic' => ['userModel' => 'Students'], 
     'Form' => ['userModel' => 'Students'] 
    ); 

} 

而且我有以下錯誤

無法找到組件類SessionComponent。

我該如何解決?

+0

'var $ name ='Student;'缺少單引號? – Pang

+0

不,在我的代碼中是正確的。好吧 – Alina

+0

不,在我的代碼中是var $ name ='Student'。我在這裏寫錯了。 – Alina

回答

0

會話已在cakePHP 3中棄用,您如何做的是爲cake2。在蛋糕3,你可以使用RequestHandeler一樣,

$this->loadComponent('RequestHandler'); 

OR

public $components = array(
     'RequestHandler' 
    ); 
then use, 
$this->request->Session()->read/write/delete/destroy(); 

希望這會爲你工作:)。

+0

我試過了你的方法,我有同樣的錯誤。 – Alina

+0

您是否已從$組件的「會話」初始化中刪除?如果是的話,那麼你可以說Cake沒有返回錯誤行。 @Alina –

+0

是的。我刪除了該錯誤消失。謝謝!但現在,我有另一個錯誤'調用未定義的方法Cake \ Controller \ Component \ AuthComponent :: login()'。 – Alina