自动将资源注入控制器
我正在尝试在我们的 Zend Framework 项目中实现依赖注入。
在之前基于 APS.NET 的项目中,我们使用了 StructureMap 并覆盖了 DefaultControllerFactory
将依赖项注入到控制器中。
我不知道在 Zend Framework 中哪里进行注入?我研究了 Zend_Controller_Plugin_Abstract
和 Zend_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
另请查看 PHP-DI,这是一个与 Zend Framework 集成的依赖注入库。它与注释一起使用。
它使您能够执行以下操作:
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: