現在、複数のプロジェクトで再利用可能なライブラリとして機能するモジュールを構築中ですが、ライブラリであるため、コントローラーは必要ありません。たとえば、私がやろうとしているのは、Marketo soap API 用の zf2 モジュールを作成することです。ユーザーは、キーと wsdl の場所を /ROOT/config/autoload/local.php に追加します。構成には、「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();
}
}
上記のコントローラー出力は、表示/テスト目的で getConfig() という関数を Client クラスに追加したため、構成の出力をダンプします。
すべての助けをありがとう。