Note that there are some explanatory texts on larger screens.

plurals
  1. POZF2 config from module with no controllers
    primarykey
    data
    text
    <p>I'm currently in the process of building a module to serve as a re-usable library throughout multiple projects, however due to it being a library there isn't a need for a controller. What I'm trying to do for instance is create a zf2 module for Marketo soap API for instance. User adds their keys and wsdl location in /ROOT/config/autoload/local.php. The configuration would include something like 'marketo'=>array(),</p> <p>Now the problem that I'm having is I want to give myself and others using the module the ability to do something like...</p> <pre><code>$marketo = new \Marketo\Client\Client(); </code></pre> <p>and inside the \Marketo\Client\Client() class have the constructor read the array key of $config['marketo'];</p> <p>I could however put all of this in an ini file, but I would prefer to keep it similar to how everything else in zf2 is configuration wise.</p> <p>So to summarize I would like to get an array key of the merged zend configuration to use inside the class something like...</p> <pre><code>class Marketo{ private $key; private $pass; public function __construct(){ $c = \Zend\Config\Config('marketo); $this-&gt;key = $c['key']; $this-&gt;pass = $c['pass']; } } </code></pre> <p><strong>============ Fully working solution as of ZF 2.1.1 per the answers below =============</strong></p> <p>Module structure looks as follows (Using a new example so I could start fresh) + indicates directory name - indicates filename</p> <pre><code>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 </code></pre> <p><strong>module.config.php</strong> <pre><code>return array( 'service_manager' =&gt; array( 'factories' =&gt; array( 'Cybersource\Client\Client' =&gt; 'Cybersource\ServiceFactory\ClientServiceFactory', ) ), 'cybersource' =&gt; array( 'Endpoint' =&gt; 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor', // test environment 'WSDL' =&gt; 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.80.wsdl', 'TXKey' =&gt; '', 'MerchID' =&gt; '', ), ); </code></pre> <p><strong>Client.php</strong> <pre><code>namespace Cybersource\Client; class Client { private $config; public function __construct($config) { $this-&gt;config = $config; } public function getConfig() { return $this-&gt;config; } } </code></pre> <p><strong>ClientServiceFactory.php</strong> <pre><code>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-&gt;get('Config'); return new Client($config['cybersource']); } } </code></pre> <p><strong>Module.php</strong> <pre><code>namespace Cybersource; use Zend\ModuleManager\Feature\ConfigProviderInterface; class Module implements ConfigProviderInterface { public function getAutoloaderConfig() { return array( 'Zend\Loader\ClassMapAutoloader' =&gt; array( __DIR__ . '/autoload_classmap.php', ) ); } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } } </code></pre> <p><strong>autoload_classmap.php</strong></p> <pre><code>&lt;?php // Generated by ZF2's ./bin/classmap_generator.php return array( 'Cybersource\Module' =&gt; __DIR__ . '/Module.php', 'Cybersource\Client\Client' =&gt; __DIR__ . '/src/Cybersource/Client/Client.php', 'Cybersource\ServiceFactory\ClientServiceFactory' =&gt; __DIR__ . '/src/ServiceFactory/ClientServiceFactory.php', ); </code></pre> <p><strong>Once the module has been activated in the application.config.php I could then use it in my IndexController on my Application Module by using:</strong></p> <pre><code>&lt;?php namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class IndexController extends AbstractActionController { public function indexAction() { $c = $this-&gt;getServiceLocator()-&gt;get('Cybersource\Client\Client'); $conf = $c-&gt;getConfig(); var_dump($conf); return new ViewModel(); } } </code></pre> <p>The above controller output would dump the output of the configuration as I added a function called getConfig() to the Client class for display / testing purposes.</p> <p>Thanks again for all the help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload