试图调用名为 "getRequest" 的 class 的未定义方法。在 SwiftMailer Symfony 中发送电子邮件

Attempted to call an undefined method named "getRequest" of class . Sendind e-mail in SwiftMailer Symfony

我正在尝试通过 Symfony 3 中的 SwiftMailer 发送电子邮件。我正在阅读该教程:http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email 我在 "Creating the form in the controller" 中遇到了问题:[= class "AppBundle\Controller\DefaultController" 的 21=]getRequest"。"

那是我在 src/AppBundle/DeffaultController:

中的 contactAction()
/**
 * @Route("/contact"), name="cont")
 */
public function contactAction()
{

    $enquiry = new Enquiry();
    $form = $this->createForm(EnquiryType::class, $enquiry);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // Perform some action, such as sending an email

            // Redirect - This is important to prevent users re-posting
            // the form if they refresh the page
            return $this->redirect($this->generateUrl('app_default_contact'));
        }
    }


    return $this->render(':default:contact.html.twig', array(
        'form' => $form->createView()
    ));
}

请帮忙!

getRequestSymfony\Bundle\FrameworkBundle\Controller\Controller 基础 class 的一种方法。它从 2.4 版本开始被弃用,并在 3.0 中被删除。

要在您的控制器中获取它,只需将其添加为参数并使用 Request class:

进行类型提示
use Symfony\Component\HttpFoundation\Request;

public function contactAction(Request $request)
{

    // ...

文档 here.

函数 getRequest() 在 symfony-3 及更高版本之后被弃用并删除。 Symfony\Bundle\FrameworkBundle\Controller\Controller;在当前(4.03)版本中仍在使用和维护。 github.com/symfony/framework-bundle

获取请求:

$request = $this->container->get('request_stack')->getCurrentRequest();

而不是 getRequest 使用 $request...