Symfony 3枝条文件与渲染控制器

时间:2017-06-01 13:27:34

标签: php symfony controller twig

我遇到了Symfony的问题。

我是一个呈现树枝页面的VideoController。

此twig页面包含另一个具有rendercontroller的twig页面。使用这个渲染控制器,路线崩溃,它说"视频"我用第一个控制器发送的变量不存在。怎么了?

这是VideoController的代码:

public function getVideo(Request $request, $id) {
    $entityManager = $this->getDoctrine()->getManager();
    $video = $entityManager->getRepository('AppBundle:Video')->getVidById($id);
    return $this->render('vids/videos.html.twig', ['video' => $video]); //Needs improvements
}

videos.html.twig:

{% block main %}
<center>
     <video controls style="width:720px;height:360px;" poster="poster.png">
     <source src="{{ video.link }}" type="video/mp4;" codecs="avc1.42E01E, mp4a.40.2" />
     </video>
     {{ include ("comments/comment.html.twig") }}
</center>
{% endblock %}

comment.html.twig:

{% block comment %}
<br><br>
  <center>
    {{ render(controller('AppBundle:Video:commentVideo')) }}
    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
</center>
{% endblock %}

CommentController:

class CommentsController extends Controller 
{
    /*
     * Check if session is valid. If so, user can comment. SECURITY SYSTEM NEEDS TO BE DEVELOPED!!!
     * @Route("/", name="comment")
     * @Method({"GET", "POST"})
     */
    public function commentVideoAction(Request $request) {
        $comment = new Comment();
        $form = $this->createFormBuilder($comment)
        ->add('text', TextType::class)
        ->add('Invia Commento', SubmitType::class)
        ->getForm();
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            $comment = $form->getData();
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($comment);
            $entityManager->flush();
            return $this->render('vids/videos.html.twig');
        }
        return $this->render('vids/videos.html.twig', array(
        'form' => $form->createView(),
    ));
    }
}

2 个答案:

答案 0 :(得分:0)

在渲染(commentVideoAction)中,您有twig文件“vids / videos.html.twig”并且您没有传递视频变量。我认为你使用的是坏模板。 (

答案 1 :(得分:-1)

我认为你的VideoController有一个错误。

尝试使用public function getVideoAction(Request $request, $id)而非getVideo

你的结构很糟糕,你的控制器中有一个循环,试试这样:

videos.html.twig:

{% block main %}
<center>
     <video controls style="width:720px;height:360px;" poster="poster.png">
     <source src="{{ video.link }}" type="video/mp4;" codecs="avc1.42E01E, mp4a.40.2" />
     </video>
     {{ render(controller('AppBundle:Video:commentVideo')) }}
</center>
{% endblock %}

comment.html.twig:

{% block comment %}
<br><br>
  <center>
    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}
</center>
{% endblock %}

CommentController:

class CommentsController extends Controller 
{
/*
 * Check if session is valid. If so, user can comment. SECURITY SYSTEM NEEDS TO BE DEVELOPED!!!
 * @Route("/", name="comment")
 * @Method({"GET", "POST"})
 */
public function commentVideoAction(Request $request) {
    $comment = new Comment();
    $form = $this->createFormBuilder($comment)
    ->add('text', TextType::class)
    ->add('Invia Commento', SubmitType::class)
    ->getForm();
    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid()) {
        $comment = $form->getData();
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($comment);
        $entityManager->flush();
        return $this->render('vids/videos.html.twig');
    }
    return $this->render('vids/comment.html.twig', array(
    'form' => $form->createView(),
));
}
}