这就是您要问的具体内容:
//REDIRECT TO LOGIN PAGE HERE!!!!!
/**
* grab Controller instance from event and use the native redirect plugin
*/
$controller = $e->getTarget();
$controller->plugin('redirect')->toUrl('/logout?' . $query);
/**
* optionally stop event propagation and return FALSE
*/
$e->stopPropagation();
return FALSE;
话虽如此,您可能需要重新考虑使用原始会话。示例(假设您已配置自定义 authAdapter):
public function checkSession($e)
{
$controller = $e->getTarget(); // grab Controller instance from event
$app = $e->getApplication();
$locator = $app->getServiceManager();
if ($controller instanceof LogoutController) return;
$authService = $locator->get('ds_auth_service');
$authAdapter = $locator->get('ds_auth_adapter');
/*
* try to authenticate
*/
if (!$authService->hasIdentity()){
$result = $authService->authenticate($authAdapter);
if ($authService->hasIdentity()) {
$this->getEventManager()->trigger('authenticate', $this, array('result' => $result));
}
}
/*
* If we are not in an exempt controller and no valid identity, redirect
*/
$isExempt = $controller instanceof \Application\Controller\LogoutController;
if (!$isExempt && !$authService->hasIdentity()) {
$query = http_build_query($result->getMessages());
$controller->plugin('redirect')->toUrl('/logout?' . $query);
$e->stopPropagation();
return FALSE;
}
// User is logged in
return TRUE;
}