Php Symfony2包括带有控制器变量的细枝

Php Symfony2包括带有控制器变量的细枝,php,html,symfony,Php,Html,Symfony,我正在学习symfony2,这是我第一次真正陷入困境,不知道该怎么办。我有3页创建,一个索引页,产品页和一个特别优惠页。所有这些页面都需要使用另一个模板中的一个动态侧栏 以下是操作的控制器: public function indexAction() ///// im showing the products form mysql here { $em = $this->getDoctrine()->getManager();

我正在学习symfony2,这是我第一次真正陷入困境,不知道该怎么办。我有3页创建,一个索引页,产品页和一个特别优惠页。所有这些页面都需要使用另一个模板中的一个动态侧栏

以下是操作的控制器:

public function indexAction()   ///// im showing the products form mysql here
    {

            $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
               return $this->render('MpShopBundle:Frontend:index.html.twig',  array(
               'products'=>$products       
               ));

    }
    public function viewAction($id) //// im showing a single product based on the id
    {

        $em = $this->getDoctrine()->getManager();
        $product = $em->getRepository('MpShopBundle:Product')->find($id);

        return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
        'product'=>$product
        ));

    }

     public function specialAction()    //// a simple page to test the sidebar
    {

            $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
               return $this->render('MpShopBundle:Frontend:special_offer.html.twig',  array(
               'products'=>$products       
               ));

    }
这是用于生成类别的边栏模板代码(仅当产品有类别时才会显示该类别):

{%用于产品中的产品%}
  • {%endfor%}
    我将边栏模板包括在我的页面上,如下所示:

    {%block sidebar%}{%include'sidebar.html.twig%}{%endblock%}

    问题:它在索引页和特价页上运行良好,但在查看页上,我收到一个未定义的变量产品错误。我想我知道这个问题,因为我在控制器中该页面的变量是product(以获取特定产品)。那么,我怎样才能让这个边栏在所有网页上正常工作呢


    已修复通过使用教程解决了该问题。如果有人有同样的问题,请阅读此文

    您需要传递此产品$products=$em->getRepository('MpShopBundle:Product')->findAll();在return语句中,返回$this->render('MpShopBundle:Frontend:product_details.html.twig',数组('product'=>$product,'products'=>$products));这不起作用,因为现在我得到了一个未定义的变量乘积。@Dominykas55我对你的观点与Jean answerI相同。我用嵌入式控制器修复了它,但我尝试了你的代码,它也起了作用。谢谢
    public function viewAction($id) //// im showing a single product based on the id
    {
    
        $em = $this->getDoctrine()->getManager();
        $product = $em->getRepository('MpShopBundle:Product')->find($id);
        $products = $em->getRepository('MpShopBundle:Product')->findAll();
    
        return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
            'product' => $product,
            'products' => $products
        ));
    
    }
    
    public function viewAction($id) //// im showing a single product based on the id
    {
    
        $em = $this->getDoctrine()->getManager();
        $product = $em->getRepository('MpShopBundle:Product')->find($id);
        $products = $em->getRepository('MpShopBundle:Product')->findAll();
    
        return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
            'product' => $product,
            'products' => $products
        ));
    
    }