Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you know from Zend Framework 1.x is an "Application Resource".</p> <p>The concept of "application resource" is replaced in Zend Framework 2 by so-called <a href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD" rel="nofollow">"services"</a> (intro <a href="http://blog.evan.pro/introduction-to-the-zend-framework-2-servicemanager" rel="nofollow">here</a>)</p> <p>An other change is modules themselves. In ZF1, a module was mainly a sub-section of your application that handled some requests. This is no longer true in ZF2: if your module defines a service or controller, that one is now accessible to all the application. There's a nice intro on some <a href="http://blog.hock.in/zf2-for-zf1-users-part-1" rel="nofollow">differences between ZF1 and ZF2 by Gary Hockin</a>.</p> <p>But anyway, modules are <em>NOT</em> self-contained. They should be developed in insulated environment and with as little dependencies as possible, but they provide cross-concerns functionality that affects all of your application.</p> <p>For your specific case of the logger, I suggest that your module always defines a logger and consumes it. What can be done to define the logger conditionally is following:</p> <pre><code>class MyModule { public function onBootstrap($e) { // $e-&gt;getTarget() is the \Zend\Mvc\Application $sm = $e-&gt;getTarget()-&gt;getServiceManager(); if (!$sm-&gt;has('some-logger-name')) { $sm-&gt;setFactory('some-logger-name', function ($sl) { return new MyLogger($sl-&gt;get('some-db')); }); } } } </code></pre> <p>You would then be able to use your 'some-logger-name' across all your application.</p> <p>A different approach is to just define the logger services and let other modules or configurations override it later on:</p> <pre><code>class MyModule { public function getConfig() { return array( 'service_manager' =&gt; array( 'factories' =&gt; array( 'some-logger-name' =&gt; 'My\Logger\Factory\ClassName' ), ), ); } } </code></pre> <p>Same is achieved with <code>getServiceConfig</code>, which is less flexible and cannot be cached, but has higher priority over <code>getConfig</code> (allows overrides) and lets you also define service factories as closures:</p> <pre><code>class MyModule { public function getServiceConfig() { return array( 'factories' =&gt; array( 'some-logger-name' =&gt; function ($sl) { return new MyLogger($sl-&gt;get('some-db')); }, ), ); } } </code></pre> <p>You could then even define a config key that has to be used to decide which logger (service name) to use.</p> <p>The concept with modules and configurations is that "last module wins", so you may define the service <code>'some-logger-name'</code> either in your module or in any module loaded before it.</p> <p>Same concepts apply also to your DB connection.</p> <p>As you can see, moving to services already gave you a certain degree of freedom. </p> <p>Keep in mind that it is not that the "Application" defines something for you: modules define your services/configs/events etc... The running application is then a composition of all these things together.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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