cakephp:登录链接不会把我带到登录页面,而是它带我登录我的登录页面
问题描述:
我在登录页面上的登录链接没有带我登录页面。相反,它将我带到了login_redirect页面的report_card页面。我在设置cookie的登录功能,然后我设置$ this - >重定向($ this-> Auth->重定向());我在设置autoRedirect false false因为我设置cookie在登录功能,cakephp:登录链接不会把我带到登录页面,而是它带我登录我的登录页面
有人可以帮我吗?提前致谢。
我的代码:
register.ctp
<?php
echo $this->Html->link('Sign Up','/merry_parents/signup',array()).' for new user |'.$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
?>
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session','Cookie');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
//var_dump($this->data);
$this->Auth->loginRedirect=array('controller'=>'merry_parents','action'=>'report_card');
$this->Auth->allow('signup','login','logout','display');
$this->Auth->authorize='controller';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
merry_parents_controller.php
<?php
class MerryParentsController extends AppController{
var $name='MerryParents';
var $helpers=array('Html','Form');
function beforeFilter(){
$this->Auth->autoRedirect=false;
parent::beforeFilter();
}
function report_card(){
}
function register(){
}
function login(){
if ($this->Auth->user()){
if (!empty($this->data)){
$this->MerryParent->id=$this->MerryParent->field('id',array(
'MerryParent.username'=>$this->data['MerryParent']['username'],
'MerryParent.password'=>$this->data['MerryParent']['password']
)
);
echo 'id: '.$this->MerryParent->id;
$this->Cookie->write('MerryParent.id',$this->MerryParent->id,false,0);
$this->set('id',$this->Cookie->read('MerryParent.id'));
}
$this->redirect($this->Auth->redirect());
}
}
report_card.ctp
<?php
var_dump($this->data);
echo 'HALLO';
if (isset($id))
echo $id;
else
echo 'id has not been set';
?>
答
其实问题出在我第一次点击登录链接时,登录链接显示正常。但是,下次我再次点击登录链接时,登录页面不会显示,而是显示report_card页面(即登录重定向页面)。原因是,我没有在我的网页上的任何地方注销按钮,所以用户一直登录。谢谢。
在merry_parents_controller.php
function register(){
$this->set('id',$this->Session->read('Auth.MerryParent.id'));
}
的寄存器功能register.ctp
<?php
if (isset($id)){
echo $this->Html->link('Logout',
array('controller'=>'merry_parents','action'=>'logout'),array());
}
else{
echo $this->Html->link('Sign Up','/merry_parents/signup',array()).' for new user |'.
$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
}
?>
现在,登录和注销工作正常。