2016-03-05 33 views
1

由於我使用Cakephp,我使用Mysql與CakePHP約定(用戶表與用戶名和密碼字段)。使用另一張表供用戶在cakephp 3登錄

出於某種原因,我不得不使用用戶名和mdputil代替密碼使用PostgreSQL數據庫與loginutil。我使用名爲Utilisateur的表格。

這裏是我把AppController.php用於驗證組件的代碼:

$this->loadComponent('Auth', [ 
     'authenticate' => [ 
      'Form' => [ 
       'fields' => [ 
        'username' => 'loginutil', 
        'password' => 'mdputil' 
       ], 
       'userModel' => 'Utilisateur' 
      ] 
     ], 
     'loginAction' => [ 
      'controller' => 'Utilisateur', 
      'action' => 'login' 
     ] 
    ]); 

在login.ctp我有兩個輸入:

$this->Form->input('loginutil'); 
$this->Form->input('mdputil'); 

我UtilisateurController.php的登錄方法與$user = $this->Auth->identify();

默認一個但是當我嘗試登錄我,$user是錯誤的。找不到用戶。

我試圖把這樣的:

$this->Auth->config('authenticate', [ 
AuthComponent::ALL => ['userModel' => 'Utilisateur'], 
'Basic', 
'Form' 
]); 

但是,這不是更好。

我在代碼中的某個地方犯了錯,或者我沒有正確讀取文檔?

感謝您的幫助。

+0

什麼是你'Utilisateur的定義「班長看起來像? –

+0

@GregSchmidt @GregSchmidt如果你的意思是'Utilisateur'控制器類,我在UtilisateurController中有一個beforeFilter,它有'$ this-> Auth-> allow(['index','login','view']);'。其餘的在AppController中。 – Geetix

+0

您的數據庫表名爲「Utilisateur」?它應該是「utilisateurs」。 –

回答

0

如果數據庫表名爲utilisateur,請指定型號/表這一行/ UtilisateursTable .PHP

namespace App\Model\Table; 

    use Cake\ORM\Table; 

    class UtilisateurTable extends Table { 

     public function initialize(array $config) { 

      $this->table('utilisateur'); // this line is important, otherwise, it assumes that your database table is utilisateurs 
     } 

    } 
+0

該模型是用蛋糕烘烤命令生成的,所以這行已經存在於'UtilisateurTable.php' – Geetix

0

嘗試在模型中使用public $useTable = 'Utilisateur';

相關問題