Note that there are some explanatory texts on larger screens.

plurals
  1. POZendFramework 2.0.0beta4 Service Manager Configuration - Difference between sub-keys
    primarykey
    data
    text
    <p><strong>EDIT:</strong> a few weeks after I posted this question Evan Coury wrote an excellent blog post on the topic of the ZF2 ServiceManager, which is where I found the best answers to my questions: <a href="http://blog.evan.pro/introduction-to-the-zend-framework-2-servicemanager" rel="nofollow">http://blog.evan.pro/introduction-to-the-zend-framework-2-servicemanager</a></p> <p>--</p> <p>I'm working on a project using ZendFramework 2.0.0beta4 and am having trouble using the Zend\ServiceManager to handle dependencies. Here is the current <a href="http://packages.zendframework.com/docs/latest/manual/en/zend.service-manager.quick-start.html" rel="nofollow">ZF2 ServiceManager documentation</a></p> <p>It lists 6 sub-keys to use when registering classes with the ServiceManager for use in our modules: <strong>abstract_factories</strong>, <strong>aliases</strong>, <strong>factories</strong>, <strong>invokables</strong>, <strong>services</strong>, and <strong>shared</strong>. If I just want to register a model class which I'm going to use in my controller to pull data from a database, which one is best? I'm specifically trying to adapt an example from the <a href="https://github.com/zendframework/ZendSkeletonApplication/" rel="nofollow">ZF2 Skeleton Application</a> shown below to my own application (DashboardTable is a model), and this example uses the <strong>factories</strong> way.</p> <pre><code>public function getServiceConfiguration() { return array( 'factories' =&gt; array( 'album-table' =&gt; function($sm) { $dbAdapter = $sm-&gt;get('db-adapter'); $table = new DashboardTable($dbAdapter); return $table; }, 'test-model' =&gt; Dashboard\Model\TestModel(), ), ); } </code></pre> <p>However, I don't know how 'db-adapter' is getting into the ServiceManager ($sm) in my separate working example from the SkeletonApplication - it has to do with an entry in the autoloaded global.php config file which has a 'db' entry containing the DB info. Because I don't know exactly how that's getting from the config file to ServiceManager, I created the simple entry below that to reduce the problem to its base components - "test-model". When I comment out the 'dashboard-table' entry and call a function from TestModel in my controller which simply outputs some text. Below is the ServiceManager config from my Module.php</p> <pre><code>&lt;?php namespace Dashboard\Model; class TestModel { public function testMethod() { $testResult = "Hello"; return $testResult; } } </code></pre> <p>Which is then passed from my controller to the view:</p> <pre><code>&lt;?php namespace Dashboard\Controller; use Zend\Mvc\Controller\ActionController; use Zend\View\Model\ViewModel; use Dashboard\Model\AlbumTable; use Dashboard\Model\TestModel; use Dashboard\Model\Dashboard; class DashboardController extends ActionController { public function indexAction() { return new ViewModel(array( 'users' =&gt; $this-&gt;getTestModel()-&gt;testMethod(), )); } public function getAlbumTable() { if (!$this-&gt;albumTable) { $sm = $this-&gt;getServiceLocator(); $this-&gt;albumTable = $sm-&gt;get('album-table'); } return $this-&gt;albumTable; } public function getTestModel() { if (!$this-&gt;testModel) { $sm = $this-&gt;getServiceLocator(); $this-&gt;testModel = $sm-&gt;get('test-model'); } return $this-&gt;testModel; } } </code></pre> <p>This code gives me a completely blank page, no errors. When I comment out the ServiceManager config from Module.php and just render a new ViewModel without any passing any arguments in my DashboardController.php file the page renders normally - loading layout.phtml and index.phtml.</p> <p>I believe I'm misunderstanding a fundamental piece of how to use the ServiceManager or possible ZF2 in general, and will greatly appreciate any insight anybody can give. This is also my first question on StackOverflow so I welcome any advice on formatting my question. Thanks.</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. 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