0

我正在研究 cakephp 2.xi 在我的数据库名称user中有一个表,它有 4 个字段id、email、password 和 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 电子邮件地址或 mobileno 登录。我不想创建另一个输入字段..我不知道我该怎么做这是我的代码

应用控制器

 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');
 }
}

用户控制器

 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');
   }
  }
  }
4

2 回答 2

0

我在这里找到了解决方案https://github.com/ceeram/Authenticate 我使用插件来实现这个功能,它工作正常..

于 2013-06-16T07:43:53.860 回答
0

在 beforefilter() 的应用程序控制器中使用此代码;

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->request->data['User']['email'] 是我发送电子邮件或手机的表单字段。

于 2016-04-28T16:19:11.383 回答