【问题标题】:Symfony 4 command and Swift Mailer issueSymfony 4 命令和 Swift Mailer 问题
【发布时间】:2019-11-30 09:57:35
【问题描述】:

我正在尝试自动调用发送电子邮件的控制器。 所以我希望能够从 symfony 命令调用这个控制器。 当我从浏览器调用 url 时,控制器功能起作用。但是当我从命令中调用该函数时,我从 Swift Mailer 收到错误,它要求我提供一个参数以构造 swift mailer ($mailer)。

我试图在我的操作调用中注入 \Swift_Mailer,但它只是显示了 Swift_Transport 的另一个参数错误。

这是我的命令文件的代码以及我的控制器操作的签名和开始:

//../src/Command/mailCommand.php
class mailCommand extends Command {

//...

public function execute (InputInterface $input, OutputInterface $output) {
        $controller = new MailController();
        //try 1
        $controller->warningMailAction();
        //try 2
        //$transport = (new \Swift_SmtpTransport('smtp://smtp.myadress.fr', 25));
        // $controller->warningMailAction(new \Swift_Mailer($transport));
        $output->writeln('coucou !');
    }
}

//---------------------------------------
//../src/Controller/MailController.php

public function warningMailAction(\Swift_Mailer $mailer) {}

我在 symfony 4.3.2 上运行,我不知道如何解决这个问题,我尝试的一切都会导致我出现更多错误。感谢每一个帮助,如果您需要更多我的代码来理解问题,请告诉我

【问题讨论】:

  • 我忘记发布错误信息了:Too few arguments to function App\Controller\MailController::warningMailAction(), 0 passed in /var/www/html/ParcAuto/src/Command/mailCommand.php on line 41 and exa ctly 1 expected
  • 为了消除你的错误,你试过$controller->warningMailAction(new \Swift_Mailer);吗?
  • 是的,我有,然后我有一条错误消息说我 Swift_Mailer 构造需要一个参数为了解决这个问题,我尝试了上面代码的第二种方法(查看 //try 2)
  • 调用这样的动作似乎可以解决邮件问题:$transport = (new \Swift_SmtpTransport('smtp://smtp.myaddress.fr', 25,null));$controller::warningMailAction(new \Swift_Mailer($transport)); 后面的错误是\FatalThrowableError: Using $this when not in object context,错误出现在我调用教义并访问我的数据库的控制器内部。
  • 这是一个完全独立的问题,所以你应该开始一个新的线程。

标签: symfony4 swiftmailer


【解决方案1】:

ControllerServiceSubscriber。这意味着在实例化时,容器会向其中注入一些基本服务。

通过调用new,您无需使用容器直接实例化它,并且它使用的服务不存在。您可以通过setContainer 方法传递Container,或者只是将控制器注入您的命令,它的实例化将由容器处理。

您只需将控制器依赖项传递给您的命令__construct

use App\Controller;

class mailCommand extends Command 
{
    private $controller;

    public function __construct(MailController $mailController)
    {
        $this->controller = $mailController;
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $this->controller->mailWarning(...);
        // Other stuff
    }
}

但是,控制器是特殊的,因为它们可以接收在其方法中注入的额外服务,就像 mailWarning 的情况一样。这发生在控制器解析阶段。由于这将通过命令调用,因此您也必须处理这种依赖关系。您也可以从容器中获取它,而不是手动实例化它:

public function __construct(MailController $mailController, \SwiftMailer $mailer)

您可以在Service Container Reference 中了解有关所有这些工作原理的更多信息。

【讨论】:

  • 感谢您的回复,我也是 symfony 的新手。所以即使我知道我的控制器可以被 symfony 视为一种服务。我完全不明白 containers 是如何工作的。如果我理解正确,我的选择是:1)通过容器调用我的 MailController。 2) 将我的控制器传递给命令,我将尝试看看如何做这些事情之一
  • 从昨天开始,我一直在查看服务容器参考,但没有成功。我会试试你的解决方案,谢谢你的帮助。
【解决方案2】:

我认为它有效。感谢您的宝贵帮助@msg。 我将在此处发布我的最终代码,以防对遇到同样问题的任何人有用。

这是我的 mailCommand.php

public function __construct(MailController $mailController, \Swift_Mailer $mailer)
{
    $this->controller = $mailController;
    $this->mailer = $mailer;
    parent::__construct();
}

protected function configure () {
    static $defaultName = 'app:mailAuto';
    $this->setName($defaultName);
    $this->setDescription("Permet d'envoyer des mails automatiquement lorsque le parc n'est pas à jour");
    $this->setHelp("Je serai affiche si on lance la commande bin/console app:mailAuto -h");
}

public function execute (InputInterface $input, OutputInterface $output) {
    $this->controller->warningMailAction($this->mailer);
    $output->writeln('Hello there !');
}

在我的控制器内部,我没有改变任何东西。 我正常使用$mailer。

感谢所有帮助过我的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2015-11-14
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2019-05-25
    • 2022-01-08
    相关资源
    最近更新 更多