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

标签 php email symfony swiftmailer

我正在尝试在 Symfony 3 中通过 SwiftMailer 发送电子邮件。我正在阅读该教程:http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email我在“在 Controller 中创建表单”中遇到了问题:“尝试调用类“AppBundle\Controller\DefaultController”的名为“getRequest”的未定义方法。”

这是我在 src/AppBundle/DefaultController 中的 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 基类的方法。它自 2.4 版本起已被弃用,并在 3.0 版本中被删除。

要将其放入 Controller 中,只需将其添加为参数并使用 Request 类对其进行类型提示:

use Symfony\Component\HttpFoundation\Request;

public function contactAction(Request $request)
{

    // ...

文档 here .

关于php - 尝试调用 类 的名为 "getRequest"的未定义方法。在 SwiftMailer Symfony 中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40981191/

相关文章:

php - 保护私有(private) API key

php - Codeception,使用 pageObject 设计模式和小 cucumber 编写验收测试

java - 获取 IBM Notes 中某个日期范围内的文档

c# - 通过 Microsoft Exchange Server 发送电子邮件

symfony - 多对多 Ajax 表单(Symfony2 表单)

symfony - 使用 Nemo/Alice 包导入日期时间

php - 提交表单两次或刷新两次

html - Foundation for Emails Inliner 工具破坏布局

symfony - 有没有更聪明的方法来禁用 Symfony2.1 中的 Doctrine2 过滤器?

javascript - 从 php 按钮运行 bash 代码