1

我正在一个 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并向我显示错误消息:“找不到用户控制器。”

4

1 回答 1

1

您尚未定义默认登录页面。默认情况下,用户模型是User,所以默认登录页面是/users/login

您需要定义loginAction$this->Auth->loginAction = '/system_admins/login';

并且可能允许login未经身份验证的用户使用该页面: $this->Auth->allow('login');

于 2013-03-08T10:03:31.130 回答