2014-10-20 141 views
1

如何在Zend Framework 2中實現註銷操作? 我正在構建我的第一個zend應用程序的婚姻網站。 當我點擊註銷鏈接用戶註銷,但他的會話沒有過期,就像當我點擊編輯配置文件或搜索配置文件的菜單時,用戶會話仍然是活動的。所以我將如何能夠實現這一點。Zend Framework 2 logoutAction

的LoginController

public function indexAction() 
{ 
$id=$this->params()->fromQuery('id'); 
    if($id!="") 
    { 
    $sm = $this->getServiceLocator(); 
     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
     $sql = "UPDATE projects SET confirmation='Y' where memcode = '$id'"; 
     $statement = $dbAdapter->query($sql); 
     $result = $statement->execute(); 
    } 
    $form = new LoginForm(); 
    $viewModel = new ViewModel(array('form' =>$form)); 
    return $viewModel; 
    } 
    public function getAuthService() 
    { 

if (! $this->authservice) { 
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); 
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,'projects','email','password', 'MD5(?)'); 
$authService = new AuthenticationService(); 
$authService->setAdapter($dbTableAuthAdapter); 
$this->authservice = $authService; 
} 
return $this->authservice; 
} 

public function processAction() 
{ 
$this->getAuthService()->getAdapter()->setIdentity($this->request->getPost('email'))->setCredential($this->request->getPost('password')); 
$result = $this->getAuthService()->authenticate(); 
if ($result->isValid()) { 
$this->getAuthService()->getStorage()->write($this->request->getPost('email')); 
return $this->redirect()->toRoute(NULL , array(
'controller' => 'login', 
'action' => 'confirm' 

)); 
} 
else { 
return $this->redirect()->toRoute(NULL , array(
'controller' => 'login', 
'action' => 'index1' 
)); 
} 
} 
public function confirmAction() 
{ 
    $user_email = $this->getAuthService()->getStorage()->read(); 
    $sm = $this->getServiceLocator(); 
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
    $sql = "SELECT name,memcode,gender,castenobar FROM projects where email = '$user_email'"; 
    $statement = $dbAdapter->query($sql); 
    $result = $statement->execute(); 
    $user = $result->current(); 
    $user_session = new Container('user'); 
    $user_name = $this->getAuthService()->getStorage()->read(); 
    $user_session->username= $user['name']; 
    $user_session->usermemcode= $user['memcode']; 
    $user_session->usergender= $user['gender']; 
    $user_session->usercastenobar= $user['castenobar']; 
    $username = $user_session->username; 
    $usergender = $user_session->usergender; 
    $usercastenobar = $user_session->usercastenobar; 
    if($usercastenobar=='Y'){ 
    $sm = $this->getServiceLocator(); 
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
$statement = $dbAdapter->query($sql); 
$result = $statement->execute(); 
$selectData = array(); 
foreach ($result as $res) { 
     $selectData[]=$res; 
     } 
    return new ViewModel(array(
      'selectData' => $selectData, 
      'user_name' => $username 
     )); 

    } 
    else 
    { 
    $sql="select * from projects where gender !='".$usergender."' and castenobar='N'"; 
    //echo $sql; 
    $sm = $this->getServiceLocator(); 
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
$statement = $dbAdapter->query($sql); 
$result = $statement->execute(); 
$selectData = array(); 
foreach ($result as $res) { 
     $selectData[]=$res; 
     } 
    return new ViewModel(array(
      'selectData' => $selectData, 
      'user_name' => $username 
     )); 

    } 

} 
public function logoutAction() 
{ 

//$this->getAuthService()->clearIdentity(); 
$storage = new \Zend\Authentication\Storage\Session(); 
$storage->clear(); 
return $this->redirect()->toRoute(NULL , array(
'controller' => 'login', 
'action' => 'index' 
)); 

} 

回答

1

你 「logoutAction()」 應該是這樣的:

public function logoutAction() 
{ 
    $name = 'Zend_Auth' ; 
    $container = new Container($name); 
    $container->getManager()->getStorage()->clear($name); 

    return $this->redirect()->toRoute(NULL , array(
       'controller' => 'login', 
       'action' => 'index' 
      )); 
} 

如果不定義會話的容器的名稱,可以使用默認名稱 'Zend_Auth的'在$ name中,否則在$ name中,您必須使用自己的名稱作爲會話容器。

您的「$ this-> redirect()...」不會註銷用戶,也不會清除'Zend_Auth'容器。它只會重定向到其他網站。用戶仍然登錄/活動。要確保用戶已登錄或註銷,您可以使用:

$auth = new AuthenticationService(); 
if ($auth->hasIdentity()) { 
    return $this->redirect()->toRoute(NULL , array(
    'controller' => 'login', 
    'action' => 'index' 
    )); 
}