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'
));
}