[英]Zend Framework 2 - replace closures by factory in Module.php
[英]Zend 2: How can I get closures in Module.php to accept parameters?
編輯:我應該解釋這是一個必須使用\\ authenticate到多個dbs的應用程序。 我在自動加載配置中定義了所有數據庫適配器,如下所示:
return array(
'db' => array(
//Default adapter retrieved with $sm->get('Zend\Db\Adapter\Adapter')
'driver' => 'Pdo_Mysql',
'dsn' => 'mysql:dbname=db1;host=myserver',
'username' => $DBuser,
'password' => $DBpass,
//'driver_options' => array(
// PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
//),
'adapters' => array(
'someDB' => array(
'driver' => 'Pdo_Mysql',
'dsn' => 'mysql:dbname=someDB;host=myserver',
'username' => $DBuser,
'password' => $DBpass,
),
'someOtherDB' => array(
'driver' => 'Pdo_Mysql',
'dsn' => 'mysql:dbname=someOtherDB;host=myserver',
'username' => $DBuser,
'password' => $DBpass,
'AppType' => 'myCustomProperty',
...
我有以下功能,我正在嘗試轉移到服務管理器:
public function getAuthService($db) {
if (! $this->authService) {
//All DBs using authentication must have a view_users view
$dbAdapter = $this->getServiceLocator()->get($db);
$authAdapter = new AuthAdapter($dbAdapter);
$authAdapter
->setTableName('view_users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
;
$authService = new AuthenticationService();
$authService->setAdapter($authAdapter);
$this->authService = $authService;
}
return $this->authService;
}
我在服務管理器中有這個,但是如何修改它以便$ myParam可以在檢索時傳遞?
public function getServiceConfig() {
return array(
'factories' => array(
'AuthService' => function($sm) {
$dbAdapter = $this->getServiceLocator()->get($myParam);
$authAdapter = new AuthAdapter($dbAdapter);
$authAdapter
->setTableName('view_users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
;
$authService = new AuthenticationService();
$authService->setAdapter($authAdapter);
return $authService;
}
)
);
}
問題是我希望能夠在從服務管理器檢索它時將$ db參數傳遞給工廠。
要明確這個問題並不是專門針對如何對多個DB進行身份驗證,因為這對我來說已經很好了,我只想將我的功能移到服務管理器中。
編輯:crisp回答了我的實際問題,但我發現我的真正問題是對如何使用Authentication類的誤解。
簡而言之,我有一個登錄控制器,提示輸入用戶信用,然后檢查用戶對所有數據庫的訪問權限,並將此信息存儲在Zend \\ Authentication \\ Storage \\ Session中。 然后從各種其他模塊,只需調用Session上的read()並檢索用戶的授權信息。
你正在尋找的是一個AbstractFactory
,它本身能夠從服務名稱確定要返回哪個服務。
來自抽象工廠的服務命名必須有一個模式,我建議你的用例使用AuthService\\DbAdapterName
模式。
考慮到這一點,你可以為它寫一個工廠,比如這個......
<?php
namespace Application\Service;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class AbstractAuthenticationServiceFactory implements AbstractFactoryInterface
{
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
// the name must begin with authservice ($name is the lowercase canonical name)
if (0 !== strpos($name, 'authservice')) {
return false;
}
// remove authservice from the name, leaving just the db name
$dbName = str_replace('authservice', '', $name);
// we can create the service only if the service manager has the db
return $serviceLocator->has($dbName);
}
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$dbName = str_replace('authservice', '', $name);
$dbAdapter = $serviceLocator->get($dbName);
$authAdapter = new AuthAdapter($dbAdapter);
$authAdapter
->setTableName('view_users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($authAdapter);
return $authService;
}
}
有了工廠后,需要將它添加到abstract_factories
鍵下的service_manager
配置中...
// module.config.php
return array(
'service_manager' => array(
'abstract_factories' => array(
'authservice' => 'Application\Service\AbstractAuthenticationServiceFactory',
),
),
);
然后,您可以使用servicelocator通過使用前面描述的模式來獲取AuthService
實例
$someDbAuth = $this->getServiceLocator()->get('AuthService\someDB');
$otherDbAuth = $this->getServiceLocator()->get('AuthService\someOtherDB');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.