我正在开发cakephp 2.x.我的问题是我不想使用用户名进行日志记录..我正在从用户那里获取电子邮件和密码,并从数据库中验证此电子邮件和密码 我的数据库名称用户中有一个表格,它有3个字段 的 ID 下, 电子邮件和 密码
这是我的代码
模型
<?php
class User extends AppModel {
public $useTable = 'user';
}
?>
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')
)
);
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');
}
}
}
login.ctp
<?php
echo $this->form->create();
echo $this->form->input('email');
echo $this->form->input('password');
echo $this->form->end('Authenticate');
?>
答案 0 :(得分:8)
您可以使用以下方式对其进行配置:
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);