2012-09-17 36 views
0

在cakephp2.0,我有domain.com但是當我退出(網址爲domain.com/logout),它會被重定向到domain.com/app/login。我不希望它重定向到domain.com/app/login,而是重定向到domain.com/logout。這些都是在我的UsersController &我的AppControllerCakePHP的 - 身份驗證註銷重定向

class AppController extends Controller { 
public $helpers = array ('Html', 'Form', 'Js' => array('Jquery'), 'Session'); 

public $components = array(
    'Session', 
    'Auth' => array(
     'loginRedirect' => array('controller' => 'posts', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'users', 'action' => 'login'), 
     'authorize' => array('Controller') // Added this line 
    ) 
); 
} 

Userscontroller

class UsersController extends AppController { 
public $components = array(

    'Auth' => array(
     'loginRedirect' => array('controller' => 'posts', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'users', 'action' => 'login'), 
     'authorize' => array('Controller') // Added this line 
    ) 
); 

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('add', 'logout', 'login'); 
    $this->Auth->deny('view'); 
} 

public function login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 

      $this->redirect('http://domain.com/thankyou'); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 
} 

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

任何幫助將是巨大的我的代碼。謝謝。

回答

5

我結束了我的退出使用這個()函數

$this->Auth->logout() 
$this->redirect(some url); 
2

您是否有註銷頁面的視圖?你在退出後試圖顯示的東西?可能發生的事情是用戶已註銷,但Cake無法顯示註銷頁面,因爲它是安全的,所以Cake重定向到登錄頁面。

如果已經啓用了安全和你想有一個頁面顯示給未登錄的用戶,您將需要在其控制器是這樣的:

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('login','logout'); 
} 
1

你檢查?

'Auth' => array(
    'loginRedirect' => array('controller' => 'posts', 'action' => 'index'), 
    'logoutRedirect' => array('controller' => 'users', 'action' => 'login'), 
) 

而且

public function logout() { 
    //$this->redirect($this->Auth->logout()); 
    //Change for : 
    //I suppose that you have a logout.ctp view in your View/Pages 
    $this->redirect(array('controller' => 'pages', 'action' => 'display', 'logout') 
} 
在魯特斯

然後

Router::connect('/logout', array('controller' => 'pages', 'action' => 'display', 'logout')); 
當然

不要忘記

$this->Auth->allow('display'