我目前正在构建一个模块以用作跨多个项目的可重用库,但是由于它是一个库,因此不需要 Controller 。例如,我想做的是为 Marketo soap API 创建一个 zf2 模块。用户在/ROOT/config/autoload/local.php 中添加他们的 key 和 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' => '',
),
);
客户端.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();
}
}
上面的 Controller 输出将转储配置的输出,因为我将一个名为 getConfig() 的函数添加到 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',
),
);
}
}
注意:我更喜欢使用 getConfig
而不是 getServiceConfig
,因为它更灵活(可覆盖),并且在您设置应用程序时缓存方法调用。
然后是 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']);
}
}
之后,您将能够通过调用以下(例如在 Controller 中)从服务定位器中拉取 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 :忽略
!
所以基本上我们在这里所做的是:
- 设置服务管理器配置以使用服务工厂来实例化我们的 Marketo 客户端
- 设置服务工厂以使用合并配置(服务名称:
'config'
)来实例化 Marketo 客户端 - 为实际应用实例添加本地配置
- 通过服务定位器检索 Marketo 服务。
关于php - 来自没有 Controller 的模块的 ZF2 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14988697/