[英]ZF2 config from module with no controllers
我目前正在構建一個模塊,作為多個項目中的可重用庫,但由於它是一個庫,因此不需要控制器。 例如,我正在嘗試做的是為Marketo soap API創建一個zf2模塊。 用戶在/ROOT/config/autoload/local.php中添加其密鑰和wsdl位置。 配置包括'marketo'=> array(),
現在我遇到的問題是我想給自己和其他人使用模塊能夠做類似......
$marketo = new \Marketo\Client\Client();
在\\ Marketo \\ Client \\ Client()類中,構造函數讀取$ config ['marketo']的數組鍵;
然而,我可以將所有這些放在一個ini文件中,但我更喜歡將它與zf2中的其他所有內容類似。
總結一下,我想獲得一個合並的zend配置的數組鍵,以便在類中使用...
class Marketo{
private $key;
private $pass;
public function __construct(){
$c = \Zend\Config\Config('marketo);
$this->key = $c['key'];
$this->pass = $c['pass'];
}
}
============根據以下答案,從ZF 2.1.1開始全面運作解決方案=============
模塊結構如下所示(使用新示例以便我可以重新開始)+表示目錄名稱 - 表示文件名
modules
- Application /* Standard setup with an IndexController */
- Cybersource /* The new module to be added */
+ config
- module.config.php
+ src
+ Cybersource
+ Client
- Client.php
+ ServiceFactory
- ClientServiceFactory.php
- Module.php
- autoload_classmap.php
module.config.php
return array(
'service_manager' => array(
'factories' => array(
'Cybersource\Client\Client' => 'Cybersource\ServiceFactory\ClientServiceFactory',
)
),
'cybersource' => array(
'Endpoint' => 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor', // test environment
'WSDL' => 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.80.wsdl',
'TXKey' => '',
'MerchID' => '',
),
);
Client.php
namespace Cybersource\Client;
class Client {
private $config;
public function __construct($config) {
$this->config = $config;
}
public function getConfig() {
return $this->config;
}
}
ClientServiceFactory.php
namespace Cybersource\ServiceFactory;
use Cybersource\Client\Client;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ClientServiceFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$config = $serviceLocator->get('Config');
return new Client($config['cybersource']);
}
}
Module.php
namespace Cybersource;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface {
public function getAutoloaderConfig() {
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
)
);
}
public function getConfig() {
return include __DIR__ . '/config/module.config.php';
}
}
autoload_classmap.php
<?php
// Generated by ZF2's ./bin/classmap_generator.php
return array(
'Cybersource\Module' => __DIR__ . '/Module.php',
'Cybersource\Client\Client' => __DIR__ . '/src/Cybersource/Client/Client.php',
'Cybersource\ServiceFactory\ClientServiceFactory' => __DIR__ . '/src/ServiceFactory/ClientServiceFactory.php',
);
一旦在application.config.php中激活了模塊,我就可以在我的應用程序模塊的IndexController中使用它:
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController {
public function indexAction() {
$c = $this->getServiceLocator()->get('Cybersource\Client\Client');
$conf = $c->getConfig();
var_dump($conf);
return new ViewModel();
}
}
上面的控制器輸出將轉儲配置的輸出,因為我將一個名為getConfig()的函數添加到Client類以進行顯示/測試。
再次感謝所有的幫助。
您應該定義ServiceFactory
來創建您的Client
。 ServiceFactory可以獲取合並的模塊配置並在您的客戶端上進行設置。 你現在有了干凈的分離,你的課程甚至可以在沒有Zend \\ Config的情況下重復使用。 如果您有許多配置選項,則可以創建一個單獨的配置類,擴展\\Zend\\StdLib\\AbstractOptions
並將其傳遞給您的客戶端。
namespace Marketo\Service;
class ClientServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
//Get the merged config of all modules
$configuration = $serviceLocator->get('Config');
$configuration = $configuration['marketo'];
$client = new \Marketo\Client\Client($configuration['key'], $configuration['pass']);
}
}
現在在服務定位器中注冊您的客戶端工廠。 在module.config.php
或Module.php
執行此Module.php
public function getServiceConfig()
{
return array('factories' => array(
'marketo_client' => 'Marketo\Service\ClientServiceFactory'
);
}
用戶現在可以從ServiceManager獲取客戶端。 所有配置都整齊地設置。
$sm->get('marketo_client');
你可能會定義一個類似如下的模塊:
<?php
namespace Marketo;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return array(
'service_manager' => array(
'factories' => array(
'Marketo\Client\Client' => 'Marketo\ServiceFactory\ClientServiceFactory',
),
),
'marketo' => array(
'key' => 'DEFAULT',
'pass' => 'DEFAULT',
),
);
}
}
注意:我更喜歡在getServiceConfig
上使用getConfig
,因為它更靈活(可重寫),並且在設置應用程序時會緩存方法調用。
然后是Marketo\\ServiceFactory\\ClientServiceFactory
:
<?php
namespace Marketo\ServiceFactory;
use Marketo\Client\Client;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ClientServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');
return new Client($config['marketo']['key'], $value['marketo']['pass']);
}
}
之后,您可以通過調用以下內容(例如在控制器中)從服務定位器中提取Marketo客戶端:
$marketoClient = $this->getServiceLocator()->get('Marketo\Client\Client');
此時,您的Marketo\\Client\\Client
無論如何都是使用key
構建的, pass
兩者都設置為DEFAULT
。
讓我們繼續並通過創建config/autoload/marketo.local.php
文件來覆蓋它(在您的應用程序根目錄中,而不是在模塊中!):
<?php
return array(
'marketo' => array(
'key' => 'MarketoAdmin',
'pass' => 'Pa$$w0rd',
),
);
這非常重要,因為你永遠不應該重新分發你的key
並pass
,所以把這個文件放到.gitignore
或svn:ignore
!
基本上我們在這里做的是:
'config'
)來實例化Marketo客戶端
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.