Syl*_*llz 2 dependency-injection symfony
更新后我有这个弃用:
Since symfony/dependency-injection 5.1: The "Symfony\Component\DependencyInjection\ContainerInterface" autowiring alias is deprecated. Define it explicitly in your app if you want to keep using it. It is being referenced by the "App\Service\ImportService" service.
Run Code Online (Sandbox Code Playgroud)
这是我的ImportService:
<?php
namespace App\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ImportService
{
private $doctrine;
private $em;
public function __construct(ContainerInterface $container)
{
$this->doctrine = $container->get('doctrine'); //needed for database queries
$this->em = $this->doctrine->getManager(); //needed for database queries
}
/** more methods here **/
}
Run Code Online (Sandbox Code Playgroud)
那么我究竟如何使它明确呢?我用谷歌搜索了一下,我认为我必须以services.yml某种方式将它添加到我的文件中。但我不确定如何+我必须为每个服务类做这件事?
我刚刚创建了一个新的 5.1 应用程序并且没有折旧。Symfony 真的不鼓励注入全局容器。所以我对它被贬值并不感到惊讶。
要修复该消息,您需要做的就是明确定义 ContainerInterface 别名:
# services.yml or yaml
services:
Symfony\Component\DependencyInjection\ContainerInterface: '@service_container'
Run Code Online (Sandbox Code Playgroud)
这应该够了吧。但是,由于您似乎要迁移到 5.1,因此您应该开始重构代码并只注入特定类需要的内容。这不是强制性的,但可以避免您遇到问题:
class ImportService
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1941 次 |
| 最近记录: |