简体   繁体   中英

Zendframework2 Dependency Injection Confusion

I'm a little confused on how DI works with ZF2. I've spent the last couple of days trying to get my head around it. While I have made some progress a lot of it still baffles me...

Using this (http://akrabat.com/getting-started-with-zend-framework-2/) tutorial I managed to get a grasp that the following:

'di' => array('instance' => array(
        'alias' => array(
            'album' => 'Album\Controller\AlbumController',
        ),
        'Album\Controller\AlbumController' => array(
            'parameters' => array(
                'albums' => 'Album\Model\Albums',
            ),
        ),

works because in our Album Controller class we have a setAlbum function. So when the DI class will call that setAlbums function and pass it the 'Album\\Model\\Albums' class.

Fine get that no problem..

Now let's look at this (which comes in the skeleton application off the zend site)

            'Zend\View\HelperLoader' => array(
            'parameters' => array(
                'map' => array(
                    'url' => 'Application\View\Helper\Url',
                ),
            ),
        ),

Now i would expect there within the Zend\\View\\HelperLoader (or an inherited class) would contain a setMap() function that the DI class would pass an array. But this appears not to be the case. As I cannot find a setMap anywhere.

My question is first what am I not understanding about the way DI works with the ZF2... But also what does the code above (about zend\\view\\helper) actually do. I mean what does injecting 'map' => array('url' => 'Application\\View\\Helper\\Url') into the Zend\\View\\HelperLoader actually do?

Thanks for any help anyone can give. I appreciate it's a beta framework and what answers I may get now not apply in a months time. But this all seems pretty fundamental and i'm just no getting it!

The DI configuration of ZF2 works indeed with the names of the arguments in the signature. It does not matter if this is done with a constructor or a explicit setter. The setter must, however, start with "set" to be recognized by Zend\\Di\\Di .

So if you have a class like this:

<?php

namespace Foo;

class Bar
{
    public function __construct ($baz) {}
    public function setSomethingElse ($bat) {}
}

You can inject both a $baz and a $bat :

'di' => array(
    'instance' => array(
        'Foo\Bar' => array(
            'parameters' => array(
                'baz' => 'Something\Here',
                'bat' => 'Something\There',
            ),
        ),
    ),
)

For Zend\\Di it does not matter what the function name exactly is, as long as it starts with "set" and the name of the argument is correct. That is why Foo\\Bar::setSomethingElse($bat) works just like Foo\\Bar::setBat($bat) .

Just make sure you name your arguments correctly. For example, it is easy to do something like this:

<?php

namespace Foo;

class Bar
{
    public function setCacheForBar ($cache) {}
    public function setCacheForBaz ($cache) {}
}

But that will not work nicely together with Zend\\Di .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM