Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 2类构造函数未获取ContainerInterface?_Php_Symfony - Fatal编程技术网

Php Symfony 2类构造函数未获取ContainerInterface?

Php Symfony 2类构造函数未获取ContainerInterface?,php,symfony,Php,Symfony,好吧,我对Symfony 2还是有点陌生,但我一直在网上试图找出我做错了什么,但是一遍又一遍的查看Symfony文档,我不明白为什么它不起作用 因此,我的应用程序中有两点需要发送两封新用户电子邮件,一封带有密码,另一封带有活动路径以验证电子邮件。我用邮枪发送电子邮件,这很好用。但是我有两个控制器可以发送这些电子邮件,我想如果我想更改/编辑它们,最好它们在同一个地方。所以我为他们建立了自己的班级 这就是问题所在,因为它不是一个控制器,我正试图“呈现”一个标准的电子邮件模板布局。而对于我的生活,我不

好吧,我对Symfony 2还是有点陌生,但我一直在网上试图找出我做错了什么,但是一遍又一遍的查看Symfony文档,我不明白为什么它不起作用

因此,我的应用程序中有两点需要发送两封新用户电子邮件,一封带有密码,另一封带有活动路径以验证电子邮件。我用邮枪发送电子邮件,这很好用。但是我有两个控制器可以发送这些电子邮件,我想如果我想更改/编辑它们,最好它们在同一个地方。所以我为他们建立了自己的班级

这就是问题所在,因为它不是一个控制器,我正试图“呈现”一个标准的电子邮件模板布局。而对于我的生活,我不明白为什么它不起作用

所以我的班级:

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
这是我的捆绑包中的服务文件,我知道它正在加载,但我有一个登录处理程序,它可以正常工作

这就是我在控制器中调用类的方式

$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
顺便说一句,我试着插入一些诱人的东西,但这给了我同样的错误

欢迎大家帮忙

我做错了什么


谢谢,

您需要从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:将整个容器传递给服务并不是一个好的做法,而是只传递它真正需要的服务。


希望这有帮助

是的,我已经读到传递整个容器是一种不好的做法,这就是为什么我开始传递“@templating”-我尝试一下,谢谢你的快速回答!这似乎工作-但它有点慢-我想这是我的虚拟机发送电子邮件通过邮枪-不知道我有一个好的配置在虚拟机上的某处。但是我应该仅仅传递@templating,而不是整个容器吗?如果这是一个如此糟糕的想法,通过整个事情,为什么symfony开发工具不温暖你?如果容器里有很多东西,我想容器里面也有,那我们为什么还要用它呢?传递容器是一种不好的做法,但它是有效的。问题在于服务的设计没有明确定义工作所需的依赖关系(例如,需要模板服务代替db连接)。
$GetNewUserEmails = new NewUserEmails();

$ActiveUserEmail = $GetNewUserEmails->SendActivePathEmail($GetNewUserID, $Token);
$PasswordEmail = $GetNewUserEmails->SendPswEmail($PlainPwd);