我在会员视图中有一个登录表单(login.ctp)。但是当我将url作为/ login,而不是显示Members视图的登录时,它显示用户视图的登录页面。
/Views/login.ctp文件
<div class="members form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Member'); ?>
<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>
App控制器文件
class AppController extends Controller {
//...
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
//...
}
我的routes.php文件
Router::connect('/', array('controller' => 'members', 'action' => 'index'));
Router::connect('/login', array('controller' => 'members', 'action' => 'login'));
我已经清除了所有的cookie。错误在哪里?
还有一件事,我甚至尝试删除用户控制器,但如果删除用户控制器,我会收到以下错误...
错误:找不到UsersController。
这是我从MembersController.php
的登录功能 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'));
}
}
}
//成员模型中的代码。
class Member extends AppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'firstname' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'lastname' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'age' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'address' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'phone' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'created_on' => array(
'datetime' => array(
'rule' => array('datetime'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
function beforeSave() {
if(isset($this->data[$this->alias]['password']))
$this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
return true;
}
}
答案 0 :(得分:1)
修改强>
经过测试的代码和流程
AppController代码
<?php
class AppController extends Controller
{
public $components = array
(
'Session',
'Auth' => array
(
'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
)
);
public function beforeFilter()
{
$this->Auth->allow('index', 'view');
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
$this->Auth->userModel = 'Member';
}
}
您的所有代码都运行正常,只需将Auth->userModel
设置为Member
即可。
<强> MemberController 强>
<?php
class MembersController extends AppController
{
var $name = 'Members';
function beforeFilter()
{
parent::beforeFilter();
}
}
?>
在成员控制器中定义beforeFilter as above.
答案 1 :(得分:1)
尝试将组件数组修改为:
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index'),
'Form' => array(
'userModel' => 'Member'
),
)
);
或者在beforeFilter()中尝试这个:
// Pass settings in using 'all'
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'Member'),
'Form',
'Basic'
);