繁体   English   中英

带有渲染控制器的Symfony 3树枝文件

[英]Symfony 3 twig file with render controller

我对Symfony有问题。

我有一个VideoController渲染树枝页面。

该树枝页面包括另一个具有渲染控制器的树枝页面。 使用此渲染控制器,路由崩溃,并说我通过第一个控制器发送的“ video”变量不存在。 怎么了?

这是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(),
    ));
    }
}

在渲染(commentVideoAction)中,您有树枝文件“ vids / videos.html.twig”,并且未传递video变量。 我认为您使用的是错误的模板。

我认为您的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(),
));
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM