2011-09-22 165 views
1

我在登錄頁面上的登錄鏈接沒有帶我登錄頁面。相反,它將我帶到了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'; 

     ?> 

回答

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

    ?> 

現在,登錄和註銷工作正常。