2011-08-22 34 views
0

我已禁用autoRedirect,所以我可以在我的用戶控制器的登錄方法中做一些額外的爵士樂,並使用會話將它們發回他們來自的地方。CakePHP登錄方法讀會話如果存在重定向

class UsersController extends AppController 
{ 
    var $name = 'Users'; 

    var $components = array('Session'); 

    function beforeFilter() 
    { 
     parent::beforeFilter(); 

     $this->Auth->allow(array('login','logout','admin_logout','admin_login')); 

     $this->Session->write('back_to', $this->referer()); 
    } 

    /** 
    * Log in 
    */ 

    function admin_login() 
    { 
     $this->set('title_for_layout', 'Log in – Admin —'); 

     if(!(empty($this->data)) && $this->Auth->user()) 
     { 

      $back_to = $this->Session->read('back_to'); 

      if($back_to) 
      { 
       $this->redirect($back_to, null, true); 
      } 
      else 
      { 
       $this->redirect($this->Auth->redirect(), null, true); 
      } 
     } 

     if($this->Auth->user()) 
     { 
      $this->redirect($this->Auth->redirect(), null, true); 
     } 

    } 

這樣的想法是,如果一個用戶會話(時間99%),然後提交表單的它會向用戶發送到前一頁,如果沒有則發送到默認loginRedirect。

注意:通過將autoRedirect設置爲false,CakePHP不再使用Auth.Redirect會話!所以存儲在那裏的值不再被應用程序使用,並且是有意的!

我遇到的問題是我的應用總是將用戶發送到儀表板,因爲上面的代碼中'This one'註釋下面的功能。如果我刪除了那個函數,那麼用戶只是一直髮送回登錄表單,但他們會被登錄!

任何人都可以幫忙嗎?

感謝

更新:這裏是我的AppController代碼:

class AppController extends Controller 
{ 
    var $components = array('Auth','Session'); 

     public function beforeFilter() 
     { 

      parent::beforeFilter(); 

      $this->Auth->authorize = 'controller'; 

      $this->Auth->autoRedirect = false; 

      $this->Auth->loginAction = array('controller'=>'users','action'=>'login','admin'=>true 
      ); 

      $this->Auth->loginRedirect = array('admin'=>true,'controller' => 'dashboard', 'action' => 'index'); 

      $this->Auth->logoutRedirect = array('admin'=>false,'controller' => 'pages', 'action' => 'display','home'); 
     } 

    function isAuthorized() 
    { 
     return true; 
    } 
} 
+0

根據這一頁:http://book.cakephp.org/view/1270/loginRedirect你應該使用默認的重定向機制來做你想做的事情。除非你想要重定向,即使最後一次不需要登錄... –

回答

1

您在重定向後不存在。試着改變你的重定向簽名:

$this->redirect($back_to, null, true); 

的第二個參數是一個狀態碼,三是是否停止處理當前的行動。這應該防止你下降到最後的重定向,我猜是正在執行的。

鑑於我們長期的評論 「討論」 下面,請嘗試調整您的admin_login()方法是這樣的:

if(!(empty($this->data)) && $this->Auth->user()) { 
    $back_to = $this->Session->read('back_to'); 

    if($back_to) { 
    $this->redirect($back_to, null, true); 
    } 
    else { 
    $this->redirect($this->Auth->redirect(), null, true); 
    } 
} 
else { 
    # Only write back_to when you arrive at the login page without data 
    $this->Session->write('back_to', $this->referer()); 
} 
+0

雖然用戶仍未被髮送到正確的位置。它們要麼被髮送到儀表板,要麼回到登錄表單,而不是存儲在會話「back_to」中的位置。 – Cameron

+0

有沒有更新?謝謝 – Cameron

+0

我的第一個問題是你確定'back_to'在會話中?沒有太多細節,但是如果登錄成功的話,代碼似乎會落入你的'else'條件。 –

0
function login(){ 
    if(!empty($this->data) && !$this->Auth->user()){ 
    $this->Session->write('back_to', $this->referer()); 
    } 

刪除您的beforeFilter該會話級>寫。並且您不需要$this->Auth->allow(array('login','logout'));

+0

仍然無法正常工作,並仍然將用戶發送到儀表板。此外,會話永遠不會更新新的值,直到表單提交,因此它在beforefilter。 – Cameron

0
if(!(empty($this->data)) && $this->Auth->user()) 
    { 
     $back_to = $this->Session->read('back_to'); 

     $auth_redirect = $this->Session->read('Auth.redirect'); 

     if($auth_redirect) 
     { 
      $this->redirect($auth_redirect, null, true); 
     } 
     else if($back_to) 
     { 
      $this->redirect($back_to, null, true); 
     } 
     else 
     { 
      $this->redirect($this->Auth->redirect(), null, true); 
     } 
    } 
    else 
    { 
     $this->Session->write('back_to', $this->referer()); 
    }