所以我在我的应用程序中有两个点需要发送两个新的用户电子邮件,一个带有密码,另一个带有活动路径来验证电子邮件。我使用MailGun发送电子邮件,这很好用。但是我有两个控制器会发送这些电子邮件,我想如果我想更改/编辑它们,最好是它们在同一个地方。所以我建立了自己的课程,让他们进去。
这是问题所在,因为它不是我试图“渲染”标准电子邮件模板布局的控制器。而对于我的生活无法弄清楚为什么它不起作用。
所以我的班级:
namespace xxxBundle\Classes;
//use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NewUserEmails {
//private $templating;
//public function __construct(EngineInterface $templating) {
// $this->templating = $templating;
//}
private $container;
public function __construct(ContainerInterface $container) {
$this->templating = $container->get('templating');
}
public function SendActivePathEmail($GetNewUserID, $Token) {
/* Send Active Path To New User - Before Password */
$Email_Header_ActiveUser = 'Access';
$Email_Content_ActiveUser = 'Please active your account, by clicking the link below.';
$Email_Content_ActiveUser .= '<br/><br/><strong>PLEASE NOTE:</strong> Your account is not active untill you have vifyied your email';
$Email_Content_ActiveUser .= '<br/><br/><p><a href="xxxxxx/active/'.$GetNewUserID.'/'.$Token.'">Active Account Link</a></p>';
$ActiveUserEmail = $this->templating->render('xxxBundle:Emails:StandardEmail.html.twig',
['EmailContent' => $Email_Header_ActiveUser,
'EmailMessage' => $Email_Content_ActiveUser],
'text/html');
return $ActiveUserEmail;
}
public function SendPswEmail($PlainPwd) {
/* Send Password To New User */
$Email_Header_NewPsw = 'Access';
$Email_Content_NewPsw = 'This is your password <strong>' .$PlainPwd. '</strong> for xxxxx login';
$PasswordEmail = $this->templating->render('xxxBundle:Emails:StandardEmail.html.twig',
['EmailContent' => $Email_Header_NewPsw,
'EmailMessage' => $Email_Content_NewPsw],
'text/html');
return $PasswordEmail;
}
} //Class End
现在这就是我在服务YML文件中的内容,
new_user_emails:
class: xxxBundle\Classes\NewUserEmails
arguments: [@service_container]
这是我的捆绑包中的服务文件,我知道正在加载我有一个登录处理程序,它没有任何问题。
这就是我在Controller中调用类的方法,
$GetNewUserEmails = new NewUserEmails();
$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);
所以我可以告诉我做得对,但是当我尝试保存用户时(保存没有任何问题)我收到以下错误,
Catchable Fatal Error: Argument 1 passed to xxxxBundle\Classes\NewUserEmails::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given
P.S。我试图插入诱人的,但这给了我同样的错误!
欢迎大家帮忙,
我做错了什么?
谢谢,
答案 0 :(得分:2)
您需要从symfony容器中检索您的服务。
试试这个:
$GetNewUserEmails = $this->get('new_user_emails');
$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);
而不是:
$GetNewUserEmails = new NewUserEmails();
$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);
PS:将整个容器传递给服务并不是一个好习惯,而只是将它真正需要的服务传递出去。
希望这个帮助