我正在尝试在 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()
));
}
请帮忙!
最佳答案
getRequest
是 Symfony\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/