Symfony2:如何在formHandler中调用模板服务?
我正在尝试使用Symfony 2(版本2.4.1)制作一个联系表单。我的表单没有实体,因为它不必在数据库中存储信息(只用于发送邮件)。在表单处理程序类中调用邮件模板的templating render(或renderView?)方法时遇到问题:Symfony2:如何在formHandler中调用模板服务?,symfony,Symfony,我正在尝试使用Symfony 2(版本2.4.1)制作一个联系表单。我的表单没有实体,因为它不必在数据库中存储信息(只用于发送邮件)。在表单处理程序类中调用邮件模板的templating render(或renderView?)方法时遇到问题: <?php // src/Open/OpcBundle/Form/Handler/Handler.php namespace Open\OpcBundle\Form\Handler; use Symfony\Component\Form\For
<?php
// src/Open/OpcBundle/Form/Handler/Handler.php
namespace Open\OpcBundle\Form\Handler;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\EngineInterface;
class ContactHandler {
protected $request;
protected $form;
protected $mailer;
protected $templating;
public function __construct(Form $form, Request $request, $mailer, EngineInterface $templating) {
$this->form = $form;
$this->request = $request;
$this->mailer = $mailer;
$this->templating = $templating;
}
public function process() {
if ('POST' == $this->request->getMethod()) {
$this->form->handleRequest($this->request);
$data = $this->form->getData();
$this->onSuccess($data);
return true;
}
return false;
}
protected function onSuccess($data) {
$message = \Swift_Message::newInstance()
->setContentType('text/html')
->setSubject($data['sujet'])
->setFrom($data['courriel'])
->setTo('me@gmail.com')
->setBody($this->templating->renderView('OpcOpenBundle:Opc:Mails/contact.html.twig',
array('ip' => $request->getClientIp(),
'nom' => $data['nom'],
'msg' => $data['msg'])
)
);
$this->get('mailer')->send($message);
$request->getSession()->getFlash()->add('success', 'Your email has been sent! Thanks!');
return $this->redirect($this->generateUrl('contact'));
}
}
因此,我尝试将模板参数放入控制器中的FormHandler调用中:
<?php
// src/Open/OpcBundle/Controller/OpcController.php
namespace Open\OpcBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Open\OpcBundle\Form\Type\ContactType;
use Open\OpcBundle\Form\Handler\ContactHandler;
class OpcController extends Controller {
public function contactAction(Request $request) {
$form = $this->get('form.factory')->create(new ContactType());
$request = $this->get('request');
$formHandler = new ContactHandler($form, $request, $this->get('mailer'), $this->get('templating'));
$process = $formHandler->process();
if ($process) {
$this->get('session')->setFlash('notice', 'Merci de nous avoir contacté, nous répondrons à vos questions dans les plus brefs délais.');
}
return $this->render("OpenOpcBundle:Opc:contact.html.twig",
array("form" => $form->createView(),
"hasError" => $request->getMethod() == 'POST' && !$form->isValid()
)
);
有什么想法吗?也许我应该使用另一种方法(更简单,没有处理程序)来使用Symfony 2制作我的联系人表单?您应该更改这些内容:
- 替换
$request=$this->get('request')代码>带有
$request=$this->getRequest()代码>
- 不确定,但我认为您也缺少对表单使用
方法getForm
您可以找到请求信息(似乎
getRequest()
已被弃用)。而您是表单信息。您缺少getForm
方法代码>未缺少$请求
。这是动作的唯一参数感谢Manolo,它可以工作。我还必须在数组中替换('ip'=>$request->getClientIp())。但它是否有文档记录?我已经更新了我的答案,添加了信息源。如果问题已经解决,请标记为已回答。
<?php
// src/Open/OpcBundle/Controller/OpcController.php
namespace Open\OpcBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Open\OpcBundle\Form\Type\ContactType;
use Open\OpcBundle\Form\Handler\ContactHandler;
class OpcController extends Controller {
public function contactAction(Request $request) {
$form = $this->get('form.factory')->create(new ContactType());
$request = $this->get('request');
$formHandler = new ContactHandler($form, $request, $this->get('mailer'), $this->get('templating'));
$process = $formHandler->process();
if ($process) {
$this->get('session')->setFlash('notice', 'Merci de nous avoir contacté, nous répondrons à vos questions dans les plus brefs délais.');
}
return $this->render("OpenOpcBundle:Opc:contact.html.twig",
array("form" => $form->createView(),
"hasError" => $request->getMethod() == 'POST' && !$form->isValid()
)
);
ContextErrorException: Notice: Undefined variable: request in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Form\Handler\ContactHandler.php line 43