2012-12-07 49 views
0

我知道这个问题已经被问过,但我已经通过几乎每一篇文章我能找到在互联网和没事就消失了解决我的问题-.-登录CakePHP中使用电子邮件和密码2.1

我试图让我的CakePHP 2.1应用程序使用他们的电子邮件(而不是用户名)和密码登录用户。另外,考虑到我使用的控制器称为“配置文件”,而不是“用户”。这有什么区别吗?总之,这里是我的代码...

AppController.php

<?php 
class AppController extends Controller { 
    public $components = array(
     'Session', 
     'Auth'=>array(
      'authorize => array('Controller'), 
      'Form'=>array(
       'fields' => array('username' => 'email'), 
       'userModel' => 'Profile' 
      ) 
     ) 
    ); 
} 
?> 

ProfilesController.php

<?php 
class ProfilesController extends AppController { 

    function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       $this->Session->setFlash('You have been signed in.', 'flash_success'); 
       $this->redirect($this->Auth->redirect()); 
      } else { 
       $this->Session->setFlash('Login failed. Your username/password was incorect.'); 
      } 
     } 
    } 

} 
?> 

login.ctp

<?php 
echo $this->Form->create(); 
echo $this->Form->input('email'); 
echo $this->Form->input('password'); 
echo $this->Form->submit('Sign In'); 
?> 

任何意见/建议,为什么这仍然登录不工作!?我很困惑。

谢谢!

回答

1

谢谢您的回答!我刚刚解决了我的问题,这实际上是一个很容易犯的错误。

实际上,我是用:

'Form' => array(
    'userModel'=>'Profile', 
    'fields' => array('username' => 'email') 
) 

本身,而不是使用像这样的 '身份验证' 参数中的:

'authenticate' => array(
    'Form' => array(
     'userModel'=>'Profile', 
     'fields' => array('username' => 'email') 
    ) 
) 

感谢您的帮助家伙!欣赏它。

2

你写了这个函数吗?

ProfilesController.php

function beforeFilter() { 
    $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
} 
+0

嘿那里:),谢谢你回到我身边!我已经在我的AppController.php中,我也只是把它放在我的ProfilesController.php以及...仍然没有区别。它不断返回我的“登录失败”的消息!任何其他想法? –

+0

“登录失败”?这可能是密码问题... 检查您的表单发送的密码字符串以及数据库中的值。他们是否是实例? – Matthieu

+0

是的,密码都很好。我使用我的注册表单创建了3个额外的用户帐户,并且他们都正确地进行了散列处理。仍然没有登录。 –

2

检查俺们我给了类似的问题

unable to login in cakephp using different table

回来,如果这不能解决

+0

嘿,伙伴。它一开始看起来并不乐观,但您的答案实际上让我的用户登录!检查我的答案,为什么我的登录不起作用。真的很感激它,谢谢你! –

+0

欢迎亚当! :) – huzefam

相关问题