4
我想爲我的ZF2 v2.2網站實現rememberme功能。 所以這是我迄今所做的: 我創建了一個服務會話管理器寫會話DB:ZF2用戶記住我不工作
'session' => array(
'remember_me_seconds' => 2419200,
'use_cookies' => true,
'cookie_httponly' => true,
),
'session_manager' => function (ServiceManager $sm) {
$adapter = $sm->get('db_adapter');
$config = $sm->get('app_config');
$sessionOptions = new Session\SaveHandler\DbTableGatewayOptions();
$sessionTableGateway = new TableGateway('tbl_session', $adapter);
$saveHandler = new Session\SaveHandler\DbTableGateway($sessionTableGateway, $sessionOptions);
$sessionConfig = new Session\Config\SessionConfig();
$sessionConfig->setCookieDomain(ACTIVE_SITE);
$sessionConfig->setCookieSecure(true);
$sessionConfig->setOptions($config['session']);
$sessionManager = new Session\SessionManager($sessionConfig, NULL, $saveHandler);
$sessionManager->start();
return $sessionManager;
},
而且使用該會話管理器,我會和AuthenticationService
:
Session\Container::setDefaultManager($sm->get('session_manager'));
'user_auth_service' => function (ServiceManager $sm) {
$authService = new \Zend\Authentication\AuthenticationService();
$session = new \Zend\Authentication\Storage\Session(null, null, $sm->get('session_manager'));
$authService->setStorage($session);
return $authService;
},
在我的登錄表單我使用記得我:
public function login(\User\Model\User $user)
{
$authAdapter = $this->getServiceLocator()->get('user_auth_adapter');
$authAdapter->setIdentity($user->username);
$authAdapter->setCredential($user->password);
/* @var $authService \Zend\Authentication\AuthenticationService */
$authService = $this->getServiceLocator()->get('user_auth_service');
$result = $authService->authenticate($authAdapter);
switch ($result->getCode()) {
case \Zend\Authentication\Result::FAILURE_IDENTITY_NOT_FOUND:
case \Zend\Authentication\Result::FAILURE_CREDENTIAL_INVALID:
return $result->getMessages();
break;
case \Zend\Authentication\Result::SUCCESS:
$user = $authAdapter->getResultRowObject(null, 'password');
$user->rolls = $this->getServiceLocator()->get('user_role_table')->getRoles($user->id);
$authService->getStorage()->write($user);
getSM()->get('session_manager')->rememberMe();
return true;
break;
default:
return 'Invalid Credential Provided !';
break;
}
}
但應用程序仍然不記得我。什麼我做錯了什麼 這裏 ???
也許你可以使用或從https://github.com/goalio/GoalioRememberMe –