問題描述
我正在開發一個 ZF2 系統,它運行良好,但是在我在其他計算機上克隆存儲庫后,出現了這個已棄用的錯誤:
I'm developing a ZF2 system and it was working very well, but after I clone the repository in other computer this deprecated error has appeared:
您正在從 ModuleControllerController 類中檢索服務定位器.請注意,ServiceLocatorAwareInterface 已棄用,將在 3.0 版中與 ServiceLocatorAwareInitializer 一起刪除.您需要更新您的類以在創建時接受所有依賴項,通過構造函數參數或設置器,并使用工廠來執行注入.在第 258 行的/home/path/project/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php 中
You are retrieving the service locator from within the class ModuleControllerController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. You will need to update your class to accept all dependencies at creation, either via constructor arguments or setters, and use a factory to perform the injections. in /home/path/project/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php on line 258
composer.json:
The composer.json:
"require": {
"php": ">=5.5",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"zendframework/zendframework": "~2.5",
"doctrine/doctrine-orm-module": "0.*",
"hounddog/doctrine-data-fixture-module": "0.0.*",
"imagine/Imagine": "~0.5.0"
在我的控制器中檢索服務時出現錯誤(擴展 ZendMvcControllerAbstractActionController):
The error appears when I retrieve the service in my controllers (extending ZendMvcControllerAbstractActionController):
$this->getServiceLocator()->get("ModuleServiceService");
在 Zend 內核中 ZendMvcControllerAbstractController 是這樣的:
In the Zend core at ZendMvcControllerAbstractController is like this:
public function getServiceLocator()
{
trigger_error(sprintf(
'You are retrieving the service locator from within the class %s. Please be aware that '
. 'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
. 'with the ServiceLocatorAwareInitializer. You will need to update your class to accept '
. 'all dependencies at creation, either via constructor arguments or setters, and use '
. 'a factory to perform the injections.',
get_class($this)
), E_USER_DEPRECATED);
return $this->serviceLocator;
}
以前只有這個:
public function getServiceLocator()
{
return $this->serviceLocator;
}
我什么都試過了,有人知道我該怎么做嗎?
I've tried everything, someone know what I've to do?
推薦答案
您無需執行任何操作,還沒有.當您升級到 ZF3 時,您將不得不更改控制器類接收其依賴項的方式.
You don't have to do anything, yet. When you upgrade to ZF3, then you will have to change how your controller class receives its dependencies.
ZF2 支持兩種依賴注入模式:通過服務定位器和通過構造函數.ZF3 刪除了按服務位置"并需要按構造函數".所有這一切都有效地改變了依賴關系的解決方式,將解決方案從及時"轉移到構建時".
ZF2 supports two dependency injection patterns: by service locator and by constructor. ZF3 removes "by service location" and requires "by constructor". All this does, effectively, is change how dependencies resolve, moving the resolution from "just in time" to "at construction".
您不能從任何地方獲得服務,而是在施工處接收它們.按照以下幾行更新您的代碼:
Instead of being able to get a service from anywhere, you instead receive them at construction. Update your code along the following lines:
namespace ModuleController;
class Controller {
public function __construct(ModuleServiceService $service) {
$this->service = $service;
}
}
在類的方法中需要$this->service
的地方使用它.
Use $this->service
where you need it in the class's methods.
然后使用控制器工廠來創建您的控制器,如下所示:
Then use a controller factory to create your controller, like so:
function ($controllers) {
$services = $controllers->getServiceLocator();
return new ModuleControllerController($services->get('ModuleServiceService'));
}
在問題 5168 和 這篇博文 討論了為什么使用服務定位器的服務注入是一種反模式.
The change is discussed in Issue 5168, and this blog post discusses why service injection with a service locator is an anti-pattern.
這篇關于已棄用:在功能系統中檢索服務定位器 - ZF2的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!