2012-09-17 34 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'