Cakephp 我在cake php上使用Auth组件,该组件不';行不通
我在cake php上使用了Auth组件,但该组件不起作用 我的桌子是:Cakephp 我在cake php上使用Auth组件,该组件不';行不通,cakephp,components,authentication,Cakephp,Components,Authentication,我在cake php上使用了Auth组件,但该组件不起作用 我的桌子是: CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL auto_increment, `username` varchar(50) default NULL, `password` varchar(100) default NULL, `role` varchar(50) default 'www', PRIMARY KEY (`id`)
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) default NULL,
`password` varchar(100) default NULL,
`role` varchar(50) default 'www',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
我的用户控制器是:
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Session');
var $components = array('Session', 'Auth');
function beforeFilter() {
$this->Auth->login($this->data);
$this->set('aya',$this->data);
$this->Auth->loginAction= array ('controller'=>'users','action'=>'login');
$this->Auth->loginRedirect = array ('controller'=>'users','actions'=>'edit','1');
$this->Auth->deny('delete');
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
我的login.ctp文件是:
if($session->check('Message.auth'))
$session->flash('auth');
echo $form->create('User', array ('action','login'));
echo $form->input('usrname');
echo $form->input('password', array ('type','password'));
echo $form->end('Login');
但这不是工作
为什么??
谢谢你的帮助。你能定义不工作吗?它给你一个错误?什么都不做 我认为,由于您只在users控制器中添加auth组件,而不是在appController中添加auth组件,这将使该组件仅在用户操作中工作。由于您在UsersController中只有登录和注销功能,并且允许使用此功能,因此该组件将不会执行任何操作,只需使您的用户/登录自动工作即可 我的建议是你把这个搬走
var $components = array('Session', 'Auth');
function beforeFilter() {
$this->Auth->login($this->data);
$this->set('aya',$this->data);
$this->Auth->loginAction= array ('controller'=>'users','action'=>'login');
$this->Auth->loginRedirect = array ('controller'=>'users','actions'=>'delete');
$this->Auth->deny('delete');
}
到您的AppController,它应该可以工作
编辑:
再次阅读您的代码时,我发现了很多错误:
$this->Auth->login($this->data)代码>你不应该使用它,阅读手册,也就是手动登录,通常是基于ajax的,每次你输入并登录所有操作时,它都会得到任何post数据。。。所以删除这一行
$this->Auth->loginDirect=array('controller'=>'users','actions'=>'delete')代码>用户登录到删除操作后,您是否发送用户?建议:尝试使用索引或其他方法
$this->Auth->deny('delete')代码>除非您将其放在allowedActions中或在已被拒绝之前使用allow('delete')。。。删除这一行,因为它是不必要的,一切都是否定的,除非另有说明
- beforeFilter()将在每个请求中调用。$this->data将包含该请求的POST数据,而不仅仅是登录信息。这就是你想要的吗
- 是否将LoginDirect设置为删除
echo$form->create('User',array('action'=>'login'); echo$form->input('username'); echo$form->input('password',array('type'=>'password')代码>
我不知道您想要实现什么,所以我无法告诉您如何编写代码。阅读手册:我没有错误,我将此部分移动到app controller中,但此程序不起作用。@aya我对我的答案做了一些编辑,请查看并告诉我它是否对您有效:DYou将loginRedirect设置为删除??这是我编辑问题的示例。