|
With dependency injection and/or service managers in place, I would like to inject dependencies into my controller. As a simple test case I made a factory for a controller. A part of my module.config.php of my Foo module:
use Foo\Controller; 'controllers' => array( The only thing I get from this config is an Zend\ServiceManager\Exception\InvalidServiceNameException thrown with the message:
A service by the name or alias "foocontrollerindexcontroller" already exists and cannot be overridden, please use an alternate name What's going on and why can't I define my controller factory? Obviously I might use another factory name and let my route use that alternative name, but I'd like to have the controller in the route specified with the FQCN.
-- Jurian Sluiman
|
|
Hi Jurian,
Why would you create a factory for your controller? You can use invokables. Please see this example: https://github.com/WalterTamboer/waltertamboer.nl/blob/master/module/Blog/config/module.config.php#L90 https://github.com/WalterTamboer/waltertamboer.nl/blob/master/module/Blog/config/module.config.php#L107 As a side note, I'm going to let go of Di and start using the service manager instead. I don't want to hijack your thread but it feels more correct to request a service when I need it instead of having it pushed to all my instances in case of construction. I hope this helps. Regards, Walter Date: Tue, 10 Jul 2012 22:44:48 +0200 From: [hidden email] To: [hidden email] Subject: [zf-contributors] Factories for controller classes: InvalidServiceNameException With dependency injection and/or service managers in place, I would like to inject dependencies into my controller. As a simple test case I made a factory for a controller. A part of my module.config.php of my Foo module:
use Foo\Controller; 'controllers' => array( The only thing I get from this config is an Zend\ServiceManager\Exception\InvalidServiceNameException thrown with the message:
A service by the name or alias "foocontrollerindexcontroller" already exists and cannot be overridden, please use an alternate name What's going on and why can't I define my controller factory? Obviously I might use another factory name and let my route use that alternative name, but I'd like to have the controller in the route specified with the FQCN.
-- Jurian Sluiman |
|
This post has NOT been accepted by the mailing list yet.
In reply to this post by Jurian Sluiman-3
Hi Jurian,
your solution should work I think, at least it works for me. Just check the name of your service - it matches the FQCN of the controller, which might cause problems with the DI fallback (?). Try omitting the 'Controller' suffix in the service name. Also the call to '$sm->getServiceLocator()' inside your factory might be unnecessary, just use directly the $sm instance. My last point is you should probably not do all this in your config file, as there might not be all autoloading in place etc. Please see me blog post at: http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/ which deals exactly with your issue and where I described what works for me (or at least what worked few ZF2 commits ago :) ).
David Lukas (dlu-gs)
www.zfdaily.com |
|
In reply to this post by Walter Tamboer
On Jul 10, 2012 10:57 PM, "Walter Tamboer" <[hidden email]> wrote:
Currently I am exploring having controllers pull from SM or injecting dependencies into my controllers with SM factories. In the pre-SM era I tried both principles with DI and (imho) I found a good balance what I'd inject and what not (simply put: hard dependencies versus soft dependencies).
So I know having invokables will work. Even better, remove those and it still works if you point your routes to a FQCN. That's what I am doing now [1], but I think pulling dependencies and injecting them with SM must both work.
[1] https://github.com/ensemble/SampleApplication/blob/master/module/Sitemap/config/module.config.php
Well, I don't want to mix DI with a DI *container*. As I see it, you can use dependency injection with Zend\ServiceManager and Zend\Di: they both support dependency injection. They also both support dependency pulling by the way.
Also I want to make clear distinction between hard (for example, a database adapter) or a soft (for example, a cache storage adapter) dependency in my services. In the example of my first post, you might consider a CRUD controller and it has a hard dependency on a database mapper and repository (which I often group in a single service class when the domain logic is simple).
PS. I anyone wants to reply on this second part of the discussion, please rename the thread (eg "SM vs DI (was: Factories for controller classes)") -- Jurian Sluiman
|
|
In reply to this post by Jurian Sluiman-3
2012/7/10 Jurian Sluiman <[hidden email]> --
I was updating my application to the latest version of the SkeletonApplication and there this part of the application config was removed.
Now I removed these lines as well, I don't have the errors anymore. I can't explain the reason, but apparently you should have these lines in your configuration :) Jurian Sluiman |
| Powered by Nabble | Edit this page |
