我正在尝试在 ZF2 应用程序中使用身份验证和会话。到目前为止,我有以下代码:
在我的 Module.php 中:
// (...) rest of code
public function getServiceConfig()
{
return array(
'factories' => array(
// (...) Other factories
// Authentication Service
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTable($dbAdapter,
'sec_user','login','password');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
),
);
}
// (...) rest of code
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器登录操作中,我有:
use Zend\Session\Container;
// (...) rest of code
public function loginAction()
{
$this->getAuthService()->getAdapter()
->setIdentity('testlogin')
->setCredential('testpass');
$auth_result = $this->getAuthService()->getAdapter()->authenticate();
if ($auth_result->isValid()) {
$session = new Container(); …Run Code Online (Sandbox Code Playgroud)