symfony 中的排队协议 - 参数太少

queuing protocol in symfony - too few arguments

你好,我遇到了这个问题,我想通过 rabbitmq 和队列制作简单的电子邮件发件人 server.Main 想法是当用户注册时他会收到他注册的电子邮件。现在我只使用测试值来查看为什么它没有通过队列。准确地说是这个错误:

[info] Received message App\Message\UserRegistrationEmail

[critical] Error thrown while handling message App\Message\UserRegistrationEmail. Removing from transport after 3 retries. Error: "Handling "App\Message\UserRegistrationEmail" failed: Too few arguments to function App\Message\MessageHandler\UserRegistrationEmailHandler::__invoke(), 1 passed in /vendor/symfony/messenger/Middleware/HandleMessageMiddleware.php on line 63 and exactly 2 expected"

老实说,我不知道我做错了什么,这是其余文件:

处理程序:

<?php

namespace App\Message\MessageHandler;

use App\Message\UserRegistrationEmail;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;


class UserRegistrationEmailHandler implements MessageHandlerInterface
{
    /**
     * @param UserRegistrationEmail $orderConfirmationEmail
     */
    public function __invoke(UserRegistrationEmail $orderConfirmationEmail){

        sleep(15);
        echo('sending email right now');//test payload for queue
    }
}
class UserRegistrationEmail
{
/**
 * @var string
 */
    private $userEmail;

    public function __construct(string $userEmail){
        $this->userEmail = $userEmail;
    }

    /**
     * @return string
     */
    public function getUserEmail(): string{
        return $this->userEmail;
    }
}

最后一个控制器:

<?php

namespace App\Controller;

use App\Form\UserType;
use App\Entity\User;
use App\Message\UserRegistrationEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

class RegistrationController extends AbstractController{
    /**
     * @Route(path="/register", name="user_registration")
     * @param MailerInterface $mailer
     * @param MessageBusInterface $bus
     */
    public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder,MessageBusInterface $bus){
        $user = new User();
        $form = $this->createForm(UserType::class, $user);

        //TODO handle the submit (will only happen on POST)
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()){

            $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            // TODO sending mail with login info
            //$mailToPass = $user->getEmail();
            $bus->dispatch(new UserRegistrationEmail("test@value.test"));
            //return $this->redirectToRoute('/register');//TODO to edit future routes
            return new Response("user has been registered");
        }
        return $this->render(
            'registration/register.html.twig',
            array('form' => $form->createView())
        );
    }
}

感谢 help/clues/ideas!

解决了,这是 amqp worker 的错误,有问题但是,也将一个功能移到了 __construct