我正在CakePHP 3.3上工作。我希望用户登录使用电子邮件或手机号码连同密码。自定义查找器不能在CakePHP 3中工作
我有email, mobile, password, etc
字段的用户表。
根据CakePHP doc,我使用自定义查找器auth
来登录。
Auth
成分AppController.php
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Dashboard',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'home'
],
'authenticate' => [
'Form' => [
'finder' => 'auth'
]
]
]);
和findAuth()
行动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;
}
和登录()动作在UsersController.php
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
增加了一个空白列username
到users
表。
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
列即使在应用程序中不需要。
我用不同的方法测试,这里没有工作too..maybe CakePHP的不适合某些版本的支持和文档是不是很够本。 –