cakephp: login link not taking me to login page instead it is taking me to loginRedirect page
My login link on register page is not taking me to login page. Instead it is taking me to report_card page which is the loginRedirect page.
In beforeFilter i've set autoRedirect to false coz' i'm setting cookies in login function and then i'm setting $this->redirect($this->Auth->redirect());
Can someone please help me? thanks in advance.
my code:
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开发者_JAVA技巧';
if (isset($id))
echo $id;
else
echo 'id has not been set';
?>
Actually the problem was when I clicked on login link for the first time, login link displays fine. But, the next time i click on login link again, login page doesn't display, instead report_card page (ie. the login redirect page) displays. The reason is, i didn't have a logout button anywhere on my webpage, so the user was logged on all the time. Thanks.
in register function of merry_parents_controller.php
function register(){
$this->set('id',$this->Session->read('Auth.MerryParent.id'));
}
in 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';
}
?>
now, login and logout works fine.
精彩评论