我遗漏了什么?
services.yml
AppBundle\Services\ItemAddService:
arguments: ['@mailer']ItemAddService类
namespace AppBundle\Services;
class ItemAddService{
private $mymailer;
public function __construct(\Swift_Mailer $mailer){
$this->mymailer = $mailer;
}
public function itemCreatedMailer(){
$message = (new \Swift_Message('Hello Email'))
->setFrom('xxx@gmail.com')
->setTo('xxx@gmail.com')
->setBody("Successfully got SwiftMailer to mail from Symfony3");
$this->mymailer->send($message);
return "Check mail";
}
}当我在控制器中调用itemCreateMailer()时,我得到如下结果
“类型错误:传递给AppBundle\Services\ItemAddService::__construct()的参数%1必须是在/home/admin-daniel/symfony-test-sites/july132017/src/AppBundle/Controller/DefaultController.php”中调用的SwiftMailer的实例
错过了一些东西..。
发布于 2017-09-04 21:29:23
您不需要使用,因为您在类名之前使用了"\“,这意味着完整的类名。看看我的服务文件https://github.com/ismail1432/eniams-website/blob/master/app/config/services.yml。之后,在您的控制器中,通过id app.send_mail调用服务,但您可以随心所欲地命名它。因此在控制器中执行该$this->get('app_send_mail')->itemCreatedMailer();
发布于 2017-09-04 19:28:15
在services.yml中试用
AppBundle\Services\ItemAddService: arguments: $mailer: '@mailer'
https://stackoverflow.com/questions/46016613
复制相似问题