2017-06-01 74 views
0

我遇到了Symfony的問題。Symfony 3的樹枝文件與渲染控制器

我有一個VideoController呈現一個樹枝頁面。

此分支頁面包含另一個具有rendercontroller的分支頁面。使用這個渲染控制器,路由崩潰,它說我用第一個控制器發送的「視頻」變量不存在。怎麼了?

這是爲視頻控制器的代碼:

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 %} 

CommentControl ler:

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(), 
    )); 
    } 
} 
+0

哪個變量會出錯? –

+0

「video」。我也在問題中插入了這個信息! – ProtoTyPus

+0

您需要將'CommentController'中的變量'$ video'傳遞給模板以及... – DarkBee

回答

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

在您的渲染(commentVideoAction)中,您有樹枝文件「vids/videos.html.twig」,並且您不傳遞視頻變量。我認爲你使用了不好的模板。 (

+0

沒有必要在這裏傳遞變量,因爲他不使用它在評論控制器 –

+0

是的,但在您的模板「videos.html.twig」,有一個視頻變量... –

+0

這裏給出的視頻控制器:'$ this - > render('vids/videos.html.twig',['video'=> $ video])' –