Jan*_*ecy 6 php authentication zend-framework2
我的模块中有两个控制器,它们都需要查看用户是否登录.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更深入的解释将受到高度重视和赞赏谢谢.
试试这个:
public function getServiceConfig()
{
return array(
'aliases' => array(
'Application\Config\DbAdapter' => 'Zend\Db\Adapter\Adapter',
'Admin\Authentication\Service' => 'Zend\Authentication\AuthenticationService',
),
'factories' => array(
'Admin\Model\PagesTable' => function ($serviceManager) {
$dbAdapter = $serviceManager->get('Application\Config\DbAdapter');
$tableGateway = new TableGateway('pages', $dbAdapter);
$pagesTable = new PagesTable($tableGateway);
return $pagesTable;
},
),
);
}
Run Code Online (Sandbox Code Playgroud)
主要注意根数组的“别名”部分,任何其他更改都只是装饰性的,您可能更喜欢按照您建议的原始方式进行操作(例如使用工厂来检索 Zend\Db\Adapter\Adapter 实例,而不是使用别名)也)。
亲切的问候,
伊势
| 归档时间: |
|
| 查看次数: |
3229 次 |
| 最近记录: |