Symfony 2如何在FormHandler中调用renderView方法?

时间:2014-04-15 16:22:48

标签: symfony-2.4

我尝试与Symfony 2.4.1联系。我的表单是一个没有实体的简单联系人。

我的处理程序中发生以下错误:

FatalErrorException: Error: Call to undefined method Symfony\Component\Form\Form::render() in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Form\Handler\ContactHandler.php line 75

表单处理程序代码:

<?php
// src/Open/OpcBundle/Form/Handler/Handler.php

namespace Open\OpcBundle\Form\Handler;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;

class ContactHandler {
    protected $request;
    protected $form;
    protected $mailer;

    public function __construct(Form $form, Request $request, $mailer) {
       $this->form = $form;
       $this->request = $request;
       $this->mailer = $mailer;
    }

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

所以问题是我没有一个renderView()方法的对象/实例在onSuccess()函数中用setBody调用模板邮件

我应该使用哪个对象/实例?

由于

PS:对不起我的英语,我是法国人!

1 个答案:

答案 0 :(得分:0)

它是Symfony \ Bundle \ TwigBundle \ TwigEngine类。服务名称是“模板”。 顺便说一句,为什么你的处理程序不是服务?