cakephp:登录链接没有带我登录页面,而是带我到loginRedirect页面

时间:2011-09-22 06:59:10

标签: cakephp cookies redirect

注册页面上的我的登录链接不会将我带到登录页面。相反,它带我到report_card页面,这是loginRedirect页面。

在beforeFilter中我将autoRedirect设置为false coz'我在登录功能中设置cookie然后我设置$ this-&gt; redirect($ this-&gt; Auth-&gt; redirect()); < / p>

有人可以帮帮我吗?提前谢谢。

我的代码:

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';

      ?>

1 个答案:

答案 0 :(得分:1)

实际上问题是我第一次点击登录链接时,登录链接显示正常。但是,下次再次点击登录链接时,不显示登录页面,而是显示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';
    }

    ?>

现在,登录和注销工作正常。