试图调用类的名为"getRequest"的未定义方法.Sendind电子邮件在SwiftMailer Symfony

mus*_*cat 2 php email swiftmailer symfony

我正试图在Symfony 3中发送SwiftMailer发来的电子邮件.我正在阅读该教程:http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-电子邮件,我在"在控制器中创建表单"中遇到了问题:"尝试调用类"AppBundle\Controller\DefaultController"的名为"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()
    ));
}
Run Code Online (Sandbox Code Playgroud)

请帮忙!

Fed*_*kun 7

getRequest是一个Symfony\Bundle\FrameworkBundle\Controller\Controller基类的方法.它从版本2.4开始被弃用,并且已在3.0中删除.

要在控制器中获取它,只需将其添加为参数并使用Request类对其进行类型提示:

use Symfony\Component\HttpFoundation\Request;

public function contactAction(Request $request)
{

    // ...
Run Code Online (Sandbox Code Playgroud)

文档在这里.