自动将资源注入控制器

发布于 2024-11-27 13:50:35 字数 1208 浏览 1 评论 0原文

我正在尝试在我们的 Zend Framework 项目中实现依赖注入。

在之前基于 APS.NET 的项目中,我们使用了 StructureMap 并覆盖了 DefaultControllerFactory将依赖项注入到控制器中。

我不知道在 Zend Framework 中哪里进行注入?我研究了 Zend_Controller_Plugin_AbstractZend_Controller_Action_Helper_Abstract 但它们似乎都不能让我注入到当前实例化的控制器中。

我希望能够像在 ASP.NET 中那样注入到当前控制器的构造函数中,但设置器是可以接受的(我猜)。

关于如何实现这个或类似的事情有什么想法吗?

最终我希望能够做这样的事情:

MyController extends Zend_Controller_Action {
  // private vars
  [...]

  public function __constructor($authenticationService, $userRepository) {
    $this->_authServ = $authenticationService;
    $this->_userRepo = $userRepository;
  }    

}

我想做一些像我对stuctureMap所做的事情:

For(authenticationService).Use(WhatEverClass);

或者也许:

$currentController->authServ = $authenticationService;
$currentController->userRepo = $userRepository;

简而言之:我们可以在哪里拦截当前的创建(或获取实例)控制器?

类似(未回答)问题此处

谢谢! /乔恩

I'm trying to implement dependency injection in our Zend Framework project.

In previous APS.NET based projects we've used StructureMap and overwritten the DefaultControllerFactory to inject the dependencies into the controllers.

I'm not sure where to do the injection in Zend Framework? I've looked into Zend_Controller_Plugin_Abstract and Zend_Controller_Action_Helper_Abstract but none of them seems to enable me to inject into the currently instantiated controller.

I would love to be able to inject into the constructor of the current controller like i do in ASP.NET, but setters are acceptable (I guess).

Any ideas as to how to accomplish this or something similar?

Ultimately i would like to be able to do something like this:

MyController extends Zend_Controller_Action {
  // private vars
  [...]

  public function __constructor($authenticationService, $userRepository) {
    $this->_authServ = $authenticationService;
    $this->_userRepo = $userRepository;
  }    

}

I would like to do something like i do for stuctureMap:

For(authenticationService).Use(WhatEverClass);

or maybe:

$currentController->authServ = $authenticationService;
$currentController->userRepo = $userRepository;

In short: Where can we intercept the creation of (or get the instance of) the current controller?

Similar (unanswered) question here

Thanks!
/Jon

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

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

发布评论

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

评论(2

夕色琉璃 2024-12-04 13:50:35

Zend Framework 首席开发人员 Matthew Weier O'Phinney 发表了一篇文章,似乎解决了将资源注入控制器的想法:

ZF Action Controller 的简单资源注入器

Zend Framework lead developer Matthew Weier O'Phinney has a post that seems to address the idea of injecting resources into controllers:

A Simple Resource Injector for ZF Action Controllers

我偏爱纯白色 2024-12-04 13:50:35

另请查看 PHP-DI,这是一个与 Zend Framework 集成的依赖注入库。它与注释一起使用。

它使您能够执行以下操作:

MyController extends Zend_Controller_Action {
    /**
     * @Inject
     * @var MyService
     */
    private $myService;

    public function helloAction() {
        return $this->myService->sayHello();
    }
}

Check out also PHP-DI, this is a dependency injection library that integrates with Zend Framework. It works with annotations.

It enables you to do things like:

MyController extends Zend_Controller_Action {
    /**
     * @Inject
     * @var MyService
     */
    private $myService;

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