Symfony2:如何在formHandler中调用模板服务?

时间:2014-04-16 16:50:39

标签: symfony

我正在尝试与Symfony 2(版本2.4.1)建立联系表单。我的表单没有实体,因为它不必在数据库中存储信息(只是为了发送邮件)。我有一个问题是在表单处理程序类中为邮件模板调用模板渲染(或者可能是renderView?)方法:

<?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'));
}
}

这会产生以下错误:

ContextErrorException: Catchable Fatal Error: Argument 4 passed to Open\OpcBundle\Form\Handler\ContactHandler::__construct() must be an instance of Symfony\Component\Templating\EngineInterface, none given, called in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Controller\OpcController.php on line 135 and defined in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Form\Handler\ContactHandler.php line 18

所以我试图将模板参数放在控制器中的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()
        )
  );      

} // - contactAction()
} // - 类

但这会产生另一个错误,不再是$ request:

ContextErrorException: Notice: Undefined variable: request in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Form\Handler\ContactHandler.php line 43

有什么想法吗?也许我应该使用另一种方法(更简单,没有处理程序)来与Symfony 2进行联系表单?

1 个答案:

答案 0 :(得分:0)

你应该改变这些:

  • $request = $this->get('request');替换为$request = $this->getRequest();

  • 不确定,但我认为您也错过了在表单中使用getForm方法。

Here您可以找到请求信息(似乎已弃用getRequest())。 here您是表单信息。