2016-02-03 16 views
0

我使用CakePHP 2.8通過MAMP在本地開發了一個應用程序,並使用Chrome進行了測試。我遵循手冊和tutorial實現了用戶使用Auth組件登錄。 一切運行良好,但是當我完成應用程序時,我嘗試在Safari和Firefox中登錄,但沒有奏效。我使用錯誤的密碼得到了認證錯誤,但使用了正確的密碼,登錄將我重定向到主頁(我的Jugadores控制器的索引)以及受Auth保護的操作永遠無法訪問,我被要求一遍又一遍地登錄就像從未創建過會話一樣。 當我將我的應用程序部署在活動服務器中時,它在沒有瀏覽器的情況下工作,這讓我覺得我做錯了什麼,但是我找不到什麼。Cake Auth組件只能在一個瀏覽器中運行

這裏是我的AppController代碼:

<?php 

App::uses('Controller', 'Controller'); 

class AppController extends Controller { 
    public $components = array(
     'Session', 
     'Auth' => array(
     'loginRedirect' => array('controller' => 'jugadores', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'jugadores', 'action' => 'index'), 
     'authError' => 'You can not access that page.', 
     'authorize' => array('Controller') 
     ) 
    ); 

    public function isAuthorized($user) { 
     return true; //for simplicity. Is it wrong? 
    } 

    public function beforeFilter() { 
     $this->Auth->allow('view','index'); 
    } 

} 
?> 

我的用戶控制器:

<?php 
App::uses('AppController', 'Controller'); 

class UsersController extends AppController { 

    public $components = array('Paginator', 'Flash', 'Session'); 

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


    public function login() { 
     if($this->request->is('post')){ 
      if($this->Auth->login()){ 
       return $this->redirect($this->Auth->redirectUrl()); 
      } else { 
       $this->Flash->set('Wrong user/password.'); 
      } 
     } 
    } 

    public function logout(){ 
     return $this->redirect($this->Auth->logout()); 
    } 

} 
?> 

登錄視圖看起來是這樣的:

<div class="users view"> 
<h2>Login</h2> 
<?php 
    echo $this->Form->create('User'); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('password'); 
    echo $this->Form->end('Enter'); 

?> 
</div> 

對於用戶數據庫我遵循CakePHP約定。字段:ID,用戶名,密碼,角色,創建,修改

編輯:我把這個在AppController中的beforeRender功能:

$this->set('current_user', $this->Auth->user()); 

而且在佈局default.thtml中印吧:

print_r($current_user); 

鉻始終打印在本地服務器上進行測試時登錄的用戶數據,但Firefox和Safari請求驗證保護動作時只打印(和帶我到登錄頁面)。 在實時服務器中,所有瀏覽器的行爲相同(記錄的用戶數據並不總是顯示)。 所以我認爲在某種程度上,Auth正在工作並創建一個會話,但並不像應該那樣工作。

+0

什麼是你的表單方法? – Progrock

+0

該方法是post。 我發現了別的東西。編輯問題 –

+0

忘掉這個。看到我的答案 –

回答

0

我嘗試在CakePHP 2.8.0-RC1的新副本中實現Auth並獲得了相同的結果。 然後我嘗試了CakePHP 2.7.9的全新副本,並且所有瀏覽器和服務器中的所有內容都按預期工作,即使將我的控制器,視圖和我的應用程序的其餘部分複製到此安裝後:D

因此..這看起來像是2.8.0-RC1版本的一個bug。

相關問題