我的模块中有两个控制器,它们都需要查看用户是否登录.Login控制器使用DbTable对用户进行身份验证,并将标识写入存储.
我正在使用> Zend\Authentication\AuthenticationService; $ auth = new AuthenticationService();
在控制器函数内部然后我在多个pageAction()上实例化它的实例
为此,我在Module.php中编写了一个函数
如下
public function getServiceConfig()
{
return array(
'factories' => array(
'Application\Config\DbAdapter' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
return $dbAdapter;
},
'Admin\Model\PagesTable' => function($sm){
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$pagesTable = new PagesTable(new TableGateway('pages',$dbAdapter) );
return $pagesTable;
},
'Admin\Authentication\Service' => function($sm){
return new AuthenticationService();
}
),
);
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,我每次都会返回新的AuthenticationService(),我觉得这很糟糕.我找不到如何获取已经实例化的服务实例,或者我必须为此编写单例类.请告知任何示例代码snipets更深入的解释将受到高度重视和赞赏谢谢.