CakePHP3でログイン機能を実装しようとしていますが、私は公式のケーキのウェブサイトの認証ページにある主要な方法とは別に多くの選択肢を既に試してきました。CakePHP3で無効なユーザ名とパスワードを表示するユーザログインアクションが機能しません
エラーは「無効なユーザー名またはパスワード」です。以下は私が使ったコードのスナップショットです。私はCakePHPの命名規則に従っているので、設定上の問題はありません。プラス問題は3.xバージョンにありますので、古いバージョンでは答えません。
ファイル:がUserController>ログインアクション
public function login() {
if($this->Auth->user()){
$this->Flash->error(__('You are already logged in!'));
return $this->redirect($this->Auth->redirectUrl());
}
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->Flash->success(__('You\'re successfully logged-in.'));
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Username or password is incorrect'));
}
}
ファイル:ビュー
<?php
echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->button('Login', ['class' => 'btn']);
echo $this->Form->end();
?>
希望のログイン、私は何でもを逃していない:のAppController>機能
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
],
'authError' => 'Did you really think you are allowed to see that?',
'authenticate' => [
'Form' => [
'fields' => ['username' => 'username']
]
],
'storage' => 'Session'
]);
$this->Auth->allow(['display']);
$this->Auth->allow(['register']);
}
ファイルを初期化しますまだ意図したとおりに動作していません。 ご意見をお待ちしております。
TIA
うん。これでうまくいきましたが、以下のコードを使用してログインステータスを確認することができません: if($ this-> Auth-> user()){ $ this-> Flash-> error(__( 'あなたはログイン済み!')); return $ this-> redirect($ this-> Auth-> redirectUrl()); } –
私の前のコメントは無視してください。実際には、この(ログイン状態チェック)コードをAppControllerで使用しようとしていましたが、私はUsersControllerで使用する必要がありました。 ありがとう! –
エラーはありますか?デバッグモードがオンになっている必要があります。 –