我正在开发cakephp 2.x.i在我的数据库名称用户中有一个表格,它有4个字段 id,电子邮件,密码和mobileNo
我的 login.ctp
中有两个字段 <?php
echo $this->form->create();
echo $this->form->input('email');
echo $this->form->input('password');
echo $this->form->end('submit');
?>
我想要的是我想从他的mobileNo登录用户(如果他输入的手机号码而不是电子邮件地址)就像facebook已经完成的那样..他可以用hi电子邮件地址登录或者手机号码.i不想要创建另一个输入字段..我不知道我怎么能在这里做我的代码
AppController的
class AppController extends Controller {
public $components = array(
'Session',
'Auth'=>array(
'loginRedirect'=>array('controller'=>'users', 'action'=>'admin'),
'logoutRedirect'=>array('controller'=>'users', 'action'=>'admin'),
'authError'=>"You can't access that page",
'authorize'=>array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)))
);
)
);
public function isAuthorized($user) {
}
public function beforeFilter() {
$this->Auth->allow('index');
}
}
UserController中
public function login()
{
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Your email/password combination was incorrect');
}
}
}
答案 0 :(得分:0)
我在这里找到了解决方案https://github.com/ceeram/Authenticate 我使用插件来实现这个功能,它工作正常..
答案 1 :(得分:0)
在beforefilter();
中的app控制器中使用此代码AuthComponent::$sessionKey = 'Auth.User';
if ($this->request->is('post') && $this->action == 'login') {
$username = $this->request->data['User']['email'];
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password'), //Default is 'password' in the userModel
// 'scope'=>array('User.status' => '1'),
'userModel' => 'User'
)
);
}else{
$this->Auth->authenticate['Form']['fields']['email'] = 'mobile';
$this->request->data['User']['mobile'] = $username;
unset($this->request->data['User']['email']);
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'mobile', //Default is 'username' in the userModel
'password' => 'password'), //Default is 'password' in the userModel
// 'scope'=>array('User.status' => '1'),
'userModel' => 'User'
)
);
}
}
$ this-&gt; request-&gt; data ['User'] ['email']是我发送电子邮件或移动电话的表单字段。