正在处理某个symfony项目,我收到了一条错误消息

正在处理某个symfony项目,我收到了一条错误消息,symfony,autowired,Symfony,Autowired,我正在尝试使用Swiftmailer在Symfony 4中发送邮件,但每次尝试发送邮件时都会出现以下错误: 无法自动连接“App\Controller\FoundController::show()”的参数$notification:它引用类“App\Entity\ContactNotification”,但不存在此类服务 这是我的FoundController: /** *@Route(“/found/{id}”,name=“found_view”,methods={“GET”,“POST”}

我正在尝试使用Swiftmailer在Symfony 4中发送邮件,但每次尝试发送邮件时都会出现以下错误:

无法自动连接“App\Controller\FoundController::show()”的参数$notification:它引用类“App\Entity\ContactNotification”,但不存在此类服务

这是我的
FoundController

/**
*@Route(“/found/{id}”,name=“found_view”,methods={“GET”,“POST”})
*@param发现$Found
*@param Request$Request
*@param ContactNotification$notification
*@返回响应
*/
公共功能显示(找到$Found、请求$Request、联系人通知$notification):响应
{   
$contact=新联系人();
$contact->setFound($found);
$form=$this->createForm(ContactType::class,$contact);
$form->handleRequest($request);
如果($form->isSubmitted()&&&$form->isValid()){
$notification->notify($contact,$found);
$this->addFlash('message','Mail sent successfully!');
返回$this->redirectToRoute('found\u view'[
'id'=>$found->getId()
]);   
}
返回$this->render('found/view.html.twig'[
'found'=>found$,
'form'=>$form->createView()
]);
}
以及
ContactNotification.php

namespace-App\Entity;
使用App\Entity\Contact;
使用App\Entity\Found;
使用App\Entity\Lost;
使用细枝\环境;
类联系人通知{
/**
*@var\Swift\u Mailer
*/
私人$mailer;
/**
*@var环境
*/
私人机构;;
公共函数构造(\Swift\u Mailer$Mailer,Environment$renderer)
{
$this->mailer=$mailer;
$this->renderer=$renderer;
}
公共功能通知(联系人$Contact,已找到$Found){
$message=(新建\Swift\u消息('Objet:',$contact->getFound()->getName())
->setFrom($contact->getEmail())
->setTo($found->getUserEmail())
->setReplyTo($contact->getEmail())
->setBody($this->renderer->render('emails/contact.html.twig'[
“联系人”=>$contact
]),'text/html');
$this->mailer->send($message);
}

Symfony应用程序使用服务容器或依赖注入容器(DIC)创建具有类似服务特征的PHP类实例,例如Swiftmailer、EntityManager或您自己编写的服务。实体不被视为服务,通常称为模型。因此,它不由容器管理。相反,它们通过Doctrine的EntityManager进行管理,后者可以找到(并更新或删除)现有模型,或者您当场创建一个新模型并保存它(使用持久化和刷新操作)。由于您的
ContactNotification
位于实体的命名空间内
App\Entity\
Symfony的服务自动连线将假定它是由条令管理的实体,而不是服务

您的问题的解决方案是,将
ContactNotification
移动到不同的文件夹和命名空间,例如
src/Notifications
,从而
namespace App\Notifications
。这应该允许服务自动连接,然后在控制器内识别

您始终可以使用调试命令检查服务是否正确自动连接:

php bin/console调试:容器联系人通知

php bin/控制台调试:自动连接
找到的
也是一个实体吗?在我看来,在symfony 4中,
实体
命名空间不是在
config/services.yaml
`App\:resource:'../src/*'exclude:'../src/{DependencyInjection,entity,Notification,Migrations,Tests,Kernel.php}中作为服务创建的’“也许你应该为这个实体创建一个特定的服务。非常感谢你的回答,我试过了,它成功了