Zendframework2 依赖注入混乱

发布于 2024-12-12 07:56:07 字数 1278 浏览 0 评论 0原文

我对 DI 如何与 ZF2 一起工作有点困惑。过去几天我一直在努力解决这个问题。虽然我已经取得了一些进展,但其中很多仍然让我困惑......

使用这个(http://akrabat.com/getting-started-with-zend-framework-2/)教程我设法掌握了以下内容:

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

有效,因为在我们的相册控制器类中我们有一个 setAlbum 函数。因此,当 DI 类将调用 setAlbums 函数并向其传递“Album\Model\Albums”类时。

很好,没问题。

现在让我们看看这个(它来自 zend 站点的骨架应用程序)

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

现在我希望 Zend\View\HelperLoader (或继承的类)中包含一个 setMap() 函数, DI 类将传递一个数组。但事实似乎并非如此。因为我在任何地方都找不到 setMap 。

我的问题首先是我不理解 DI 与 ZF2 一起工作的方式......而且上面的代码(关于 zend\view\helper)实际上是做什么的。我的意思是注入“map”=>是什么? array('url' => 'Application\View\Helper\Url') 放到 Zend\View\HelperLoader 中实际上是做什么的?

感谢任何人可以提供的帮助。我很欣赏它是一个测试版框架,我现在得到的答案在几个月后并不适用。但这一切似乎都很基本,但我就是不明白!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-12-19 07:56:07

ZF2 的 DI 配置确实适用于签名中的参数名称。这是通过构造函数还是显式设置器完成的并不重要。然而,setter 必须以“set”开头才能被 Zend\Di\Di 识别。

因此,如果您有这样的类:

<?php

namespace Foo;

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

您可以注入 $baz$bat

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

对于 Zend\Di 来说这并不重要函数名到底是什么,只要它以“set”开头并且参数名称正确即可。这就是为什么 Foo\Bar::setSomethingElse($bat) 的工作方式与 Foo\Bar::setBat($bat) 类似。

只要确保你正确命名你的论点即可。例如,很容易做这样的事情:

<?php

namespace Foo;

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

但这不能与 Zend\Di 很好地配合。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文