Twilio Symfony - 控制器必须 return \"Symfony\Component\HttpFoundation\Response\" 但 returned Twilio\\TwiML\\VoiceResponse。”

Twilio Symfony - The controller must return a \"Symfony\\Component\\HttpFoundation\\Response\" but returned Twilio\\TwiML\\VoiceResponse."

我想使用 Symfony5ApiPlatform

实现 Twilio 浏览器到浏览器调用

我正在关注这个教程:

https://www.twilio.com/docs/voice/client/tutorials/calls-between-devices?code-sample=code-generate-twiml-from-client-parameters-3&code-language=PHP&code-sdk-version=5.x

我有这个功能,这就是我想要在

上配置我的 TwiML 应用程序的功能
    /**
     * @Route("/twilio/handle/twiml/{clientId}", name="twilio_handl_twiml")
     * @param $clientId
     * @return VoiceResponse
     */
    public function handleTwiml($clientId): VoiceResponse
    {
        /** @var Client $client */
        $client = $this->clientRepository->findOneBy(['id' => 11]);
        $to = $client->getUser()->getLastName().$client->getUser()->getId();


        $voiceResponse = new VoiceResponse();
        $number = htmlspecialchars($to);
        $dial = $voiceResponse->dial(null, array('callerId' => '+15017122661'));

        if (isset($to)) {
            if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
                $dial->number($number);
            } else {
                $dial->client($number);
            }
        } else {
            $voiceResponse->say('There has been an issue. Thanks for calling!');
        }

        return $voiceResponse;
    }

并且我在 "get" 部分中的一个实体上将其声明为自定义路由:


 *          "twilio_handl_twiml"={
 *            "path"="/twilio/handle/twiml/{clientId}",
 *            "controller"="TwilioController:class"
 *          },

现在函数创建了一个合适的 VoiceResponse 对象

但是当我调用此路由时,我收到以下错误消息:

The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned an object of type Twilio\TwiML\VoiceResponse.

现在有没有人知道为什么我不能 return 我想要的任何类型的 Response 自定义路线?

我真的不明白为什么框架会将此声明为错误

如果有人能帮助我更好地理解这个错误,我将不胜感激

谢谢!

这里是 Twilio 开发人员布道者。

正如@Cerad 在评论中所说,您需要使用派生自 Symfony Response 对象的对象进行响应。

我没有使用过 Symfony,所以如果这有误,请原谅,但我认为您可以将您的处理程序更新为以下内容,它可能会起作用:

    use Symfony\Component\HttpFoundation\Response;

    /**
     * @Route("/twilio/handle/twiml/{clientId}", name="twilio_handl_twiml")
     * @param $clientId
     * @return Response
     */
    public function handleTwiml($clientId): VoiceResponse
    {
        /** @var Client $client */
        $client = $this->clientRepository->findOneBy(['id' => 11]);
        $to = $client->getUser()->getLastName().$client->getUser()->getId();


        $voiceResponse = new VoiceResponse();
        $number = htmlspecialchars($to);
        $dial = $voiceResponse->dial(null, array('callerId' => '+15017122661'));

        if (isset($to)) {
            if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
                $dial->number($number);
            } else {
                $dial->client($number);
            }
        } else {
            $voiceResponse->say('There has been an issue. Thanks for calling!');
        }
        
        $response = new Response(
            $voiceResponse->asXML(),
            Response::HTTP_OK,
            ['content-type' => 'application/xml']
        );

        return $response;
    }

这里的关键是用语音响应的内容构建 Symfony 响应 ($voiceResponse->asXML()),并将内容类型设置为 application/xml