Symfony 3错误:变量“存储库”不存在

时间:2017-03-10 15:49:34

标签: php symfony twig runtime

我已经坚持了好几天!我能够将表单数据保存到数据库,但我无法检索该数据以显示它。我总是得到500内部服务器错误 - Twig_Error_Runtime(变量“存储库”不存在)。这是我的控制器:

class FormController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // create a message instance
        $message = new Message();

        //assign some placeholder data
        $message->setName('Billy');
        $message->setAddy('ok@hotmail.com');
        $message->setSubject('Notice');
        $message->setBody('Practice Delayed');

        //method creates and renders form
        $form = $this->createFormBuilder($message)
            ->add('name', TextType::class)
            ->add('addy', TextType::class)
            ->add('subject', TextType::class)
            ->add('body', TextType::class)
            ->add('save', SubmitType::class, array('label' => 'Send', 'attr' => array('class' => 'btn btn-danger btn-lg')))
            ->getForm();

        //method checks if the form is submitted
        $form->handleRequest($request);

        //if form is submitted and data is valid...
        if ($form->isSubmitted() && $form->isValid()) {

            //fetch data
            $name = $form->get('name')->getData();
            $addy = $form->get('addy')->getData();
            $subject = $form->get('subject')->getData();
            $body = $form->get('body')->getData();

            //set data
            $message->setName($name);
            $message->setAddy($addy);
            $message->setSubject($subject);
            $message->setBody($body);

            //entity manager
            $em =  $this->getDoctrine()->getManager();
            $repository = $em->getRepository('AppBundle:Message')->findAll();
            $em->persist($message);
            $em->flush();

            if (!$repository) {
                throw $this->createNotFoundException(
                    'No messages'
                );
            }
            return $this->render('default/output.html.twig', array('form' => $form->createView(),
            ));
        }

        return $this->render('default/index.html.twig', array('form' => $form->createView(),
        ));
    }

这是我的index.html.twig文件,该文件很好用,可以让我将数据保存到数据库中:

{% extends 'base.html.twig' %}

{% block body %}
            {% block form_row %}
            {% spaceless %}
                <div id="wrapper">
                    <div id="container">
                        <div class="form_row">
                            {{ form(form) }}
                        </div>
                    </div>
                </div>
            {% endspaceless %}
        {% endblock form_row %}
{% endblock %}

但这是我的output.html.twig文件,当我点击“发送”时,它会给我错误:

{% extends 'base.html.twig' %}

    {% block body %}
        <table class="table table-striped">
            <thead>
            <tr>
                <th>Genus</th>
                <th># of species</th>
            </tr>
            </thead>
            <tbody>
                {% for key in repository %}
                    <tr>
                        <td>{{ key.id }}</td>
                        <td>{{ key.name }}</td>
                        <td>{{ key.addy }}</td>
                        <td>{{ key.subject }}</td>
                        <td>{{ key.body }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endblock %}

1 个答案:

答案 0 :(得分:1)

您应该将存储库变量提供给Twig模板。 例如:

return $this->render('default/index.html.twig', array('form' =>  $form->createView(), 'repository' => $repository));