Quantcast

DI and Controllers in ZF2

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

DI and Controllers in ZF2

Philip Gabbert
I read around that DI works on our controllers, just by supplying our
instance, and our DI parameters. However, I can't seem to get it working
with 2.2.2. I added my controller to allowed_controllers, and it's still
not working.

I've also tried using 'version' in place of
"Firmware\Controller\FirmwareVerisonController" in case DI was dependent on
the alias names, vs the actual class name. setFirmwarePlatform is never
called, nothing is ever passed to my controller constructor.

The route and controller works, but the DI doesn't.

My config looks like so:

<?php
//....
    'router' => array(
        'routes' => array(
            'rest-api' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/api/:version/rest/:controller',
                    'criteria' => array(
                        'version' => '\d+\.\d+',
                        'controller' => '[a-z][\w]+',
                    ),
                ),
            ),
        ),
    ),
//....
   'controllers' => array(
        'invokables' => array(
              'version' => 'Firmware\Controller\FirmwareVersionController',
        ),
   ),
//....
    'di' => array(
        'allowed_controllers' => array(
            'Firmware\Controller\FirmwareVersionController',
        ),
        'instance' => array(
            'Firmware\Controller\FirmwareVersionController' => array(
                'parameters' => array(
                    'firmwarePlatform' => 'Firmware\PlatformObject',
                ),
            ),
        ),
    ),


---
Philip
[hidden email]
http://www.gpcentre.net/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DI and Controllers in ZF2

weierophinney
Administrator
On Fri, Aug 23, 2013 at 4:52 PM, Philip G <[hidden email]> wrote:
> I read around that DI works on our controllers, just by supplying our
> instance, and our DI parameters. However, I can't seem to get it working
> with 2.2.2. I added my controller to allowed_controllers, and it's still
> not working.

Just for argument's sake, why are you using Zend\Di instead of
Zend\ServiceManager? Is there a compelling reason?

I tend to steer people away from Zend\Di, as the configuration gets
tricky, and we have a lot of unsolved edge cases in the code base. On
the other hand, Zend\ServiceManager is well-maintained and
well-understood.



--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DI and Controllers in ZF2

Philip Gabbert
On Fri, Aug 23, 2013 at 3:05 PM, Matthew Weier O'Phinney
<[hidden email]>wrote:

> Just for argument's sake, why are you using Zend\Di instead of
> Zend\ServiceManager? Is there a compelling reason?
>
>
Purely for the sake of a co-worker's request and desire. He didn't like
hard coding of a ServiceManager alias name, even if it was an alias. He
comes from a Java background and rather see DI than SM aliases.

However, if there is a compelling reason to use SM over DI with ZFW, I
could easily convince him otherwise.

In my example, I originally use $sm->get('Firmware\PlatformObject'); and it
does work perfectly. And he just rather see: $this->getPlatformObject()
where platformObject is injected into our controller by the framework. I
wanted to compare the two different approaches, and select the best option.

---
Philip
[hidden email]
http://www.gpcentre.net/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DI and Controllers in ZF2

Michael Gooden
Hi Phillip G,

The easiest way to get the behaviour your co-worker desires is to use
service manager factories. You can create a factory for your controller,
and inject the service there. Then you will be able to do
$this->getPlatformObject() as expected.

Kind Regards,

Michael Gooden


On 24 August 2013 00:21, Philip G <[hidden email]> wrote:

> On Fri, Aug 23, 2013 at 3:05 PM, Matthew Weier O'Phinney
> <[hidden email]>wrote:
>
> > Just for argument's sake, why are you using Zend\Di instead of
> > Zend\ServiceManager? Is there a compelling reason?
> >
> >
> Purely for the sake of a co-worker's request and desire. He didn't like
> hard coding of a ServiceManager alias name, even if it was an alias. He
> comes from a Java background and rather see DI than SM aliases.
>
> However, if there is a compelling reason to use SM over DI with ZFW, I
> could easily convince him otherwise.
>
> In my example, I originally use $sm->get('Firmware\PlatformObject'); and it
> does work perfectly. And he just rather see: $this->getPlatformObject()
> where platformObject is injected into our controller by the framework. I
> wanted to compare the two different approaches, and select the best option.
>
> ---
> Philip
> [hidden email]
> http://www.gpcentre.net/
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DI and Controllers in ZF2

weierophinney
Administrator
In reply to this post by Philip Gabbert
On Fri, Aug 23, 2013 at 5:21 PM, Philip G <[hidden email]> wrote:

> On Fri, Aug 23, 2013 at 3:05 PM, Matthew Weier O'Phinney
> <[hidden email]>wrote:
>
>> Just for argument's sake, why are you using Zend\Di instead of
>> Zend\ServiceManager? Is there a compelling reason?
>>
>>
> Purely for the sake of a co-worker's request and desire. He didn't like
> hard coding of a ServiceManager alias name, even if it was an alias. He
> comes from a Java background and rather see DI than SM aliases.
>
> However, if there is a compelling reason to use SM over DI with ZFW, I
> could easily convince him otherwise.
>
> In my example, I originally use $sm->get('Firmware\PlatformObject'); and it
> does work perfectly. And he just rather see: $this->getPlatformObject()
> where platformObject is injected into our controller by the framework. I
> wanted to compare the two different approaches, and select the best option.

So, several things are getting conflated here.

First, the way we recommend using the ServiceManager is as an
Inversion of Control (IoC) container -- which, if done correctly, ends
up looking exactly like your co-worker wants. You create service
definitions for each object, and any object that needs dependencies
either has a factory, or makes use of initializers, in order to inject
them.

That addresses the `$this->getPlatformObject()` use case -- as your
factory and/or initializers are injecting it for you.

Second, regarding aliases: we typically recommend using fully
qualified class names in the service manager as well, with the one
exception being several of the well-known MVC services (Application,
ControllerManager, etc.). We recommend using interface names for
aliases, which then allows you to switch implementations without
needing to replace existing factories.

Now, for the compelling reasons to use SM over DI in ZF2:

- it's more performant
- configuration is more straight-forward (you're typically writing
factories in PHP, rather than creating metadata trees)
- completely predictable (DI, due to the nature of the lookups, often is not)
- easy to debug (put breakpoints in your factories, vs debugging framework code)
- used everywhere (all plugin manager implementations are specific SM instances)



--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Loading...