我正在一个 cakephp 项目中实现身份验证。但我不想使用默认的“用户”表。我想使用“system_admins”表。我在这里阅读了一个教程:http: //book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers。
这是我的控制器(SysemAdminController.php)
<?php
App::uses('AppController', 'Controller');
class SystemAdminsController extends AppController {
// index, add, edit, delete actions
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'SystemAdmin'),
'Form',
'Basic'
);
}
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
}
else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
}
这是模型(SystemAdmin.php)
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
// validates options
public function beforeSave($options = array()) {
if(isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
}
这是视图(login.ctp)
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('SystemAdmin'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
但是每当我尝试访问http://mydomain.com/system_admins/login 时,它总是将我重定向到http://mydomain.com/users/login并向我显示错误消息:“找不到用户控制器。”