【问题标题】:Attempted to call an undefined method named "getRequest" of class . Sendind e-mail in SwiftMailer Symfony试图调用类的名为“getRequest”的未定义方法。在 SwiftMailer Symfony 中发送电子邮件
【发布时间】:2017-04-20 06:08:02
【问题描述】:

我正在尝试通过 Symfony 3 中的 SwiftMailer 发送电子邮件。我正在阅读该教程:http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email,我在“在控制器中创建表单”中遇到了问题:“尝试调用“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()
    ));
}

请帮忙!

【问题讨论】:

  • 你的文件名和类名是否相同,命名空间设置是否正确?你的类是否扩展了根控制器,让你可以访问这些方法?

标签: php email symfony swiftmailer


【解决方案1】:

getRequestSymfony\Bundle\FrameworkBundle\Controller\Controller 基类的方法。自 2.4 版起已弃用,并已在 3.0 版中删除。

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

use Symfony\Component\HttpFoundation\Request;

public function contactAction(Request $request)
{

    // ...

文档here

【讨论】:

  • 你能告诉我我应该在代码中改变什么,因为我真的不知道。 :(
  • 如何更改该行? $request = $this->getRequest();
  • 我的回答太糟糕以至于难以理解吗?只需删除 $request = $this->getRequest(); 并使用 Request 类添加类型提示,如上所述
  • 非常感谢。对不起我的“不理解”,但我是新手。有用! :)
【解决方案2】:

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

获取请求:

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

【讨论】:

    【解决方案3】:

    而不是getRequest 使用$request....

    【讨论】:

      猜你喜欢
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2016-02-23
      • 1970-01-01
      相关资源
      最近更新 更多