私はCakePHP 3.3で作業しています。 メールまたはモバイル番号とパスワードのいずれかを使用してログインするようにしてください。カスタムファインダーがCakePHP 3で動作していません
私はユーザテーブルにemail, mobile, password, etc
フィールドを持っています。
CakePHP docによれば、カスタムファインダーauth
を使用してログインしています。
Auth
UsersController.php
UsersTable.php
public function findAuth(Query $query, array $options)
{
$query
->select(['id', 'email', 'mobile', 'password'])
->where(['Users.email' => $options['login']])
->orWhere(['Users.mobile' => $options['login']]);
return $query;
}
とログイン()アクションでAppController.php
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Dashboard',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'home'
],
'authenticate' => [
'Form' => [
'finder' => 'auth'
]
]
]);
とfindAuth()
アクションコンポーネント
public function login() { if ($this->request->is('post')) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); return $this->redirect($this->Auth->redirectUrl()); } $this->Flash->registerError(__('Invalid Credentials, try again'), ['key' => 'registration']); } }
login.ctp
ビューは
<?php
echo $this->Form->create();
echo $this->Form->input('login');
echo $this->Form->input('password');
echo $this->Form->submit();
echo $this->Form->end();
?>
が含まれています。しかし、これはInvalid Credentials, try again
Update 2
を作業して印刷されていないusers
表に空欄username
を追加しました。
login.ctp
<?php
echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->submit();
echo $this->Form->end();
?>
AppController.php:AuthComponentの
同上
findAuth()
public function findAuth(Query $query, array $options)
{
$query
->orWhere(['Users.email' => $options['username']])
->orWhere(['Users.mobile' => $options['username']]);
return $query;
}
これで動作します。しかし、アプリケーションで不要な場合でも、なぜusername
列を使用するように強制しますか?
はいくつかのバージョンとドキュメントのためにサポートしていませんが、このために非常に十分ではありません。 –