我在cakephp項目中實現身份驗證。但我不想使用默認的「用戶」表。我想使用「system_admins」表。我在這裏閱讀教程:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers。CakePHP中的身份驗證:不使用默認的「用戶」表
這裏是我的控制器(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並向我顯示錯誤消息:「UsersController無法找到。」
設置$這個 - > Auth-> loginRedirect和$這個 - > Auth-> logoutRedirect(http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$loginRedirect) – kicaj 2013-03-08 08:44:23