Symfony 3:在另一个方法中转发一个方法的结果

Symfony 3 : Forwarding a result of a method in an other method

我正在尝试将此方法的结果插入同一控制器中的另一个方法:

public function categorieAction(Request $request, string $categorie) {

        $categories = $this->getDoctrine()->getRepository(Categorie::class)->findAll();

        $criteria = 'p.subcat_id=sub.id and sub.categorie_id=cat.id and cat.nom like "' . str_replace('-', ' ', $categorie) . '"';
        $em = $this->getDoctrine()->getManager();

        $query1 = 'SELECT p.*,(SELECT min(h.prix) FROM Histprix h,Urlproduit u
                    WHERE h.urlproduit_id=u.id and u.produit_id=p.id  and p.status=1) as "prixmin",(SELECT max(h.prix) FROM Histprix h,Urlproduit u
                    WHERE h.urlproduit_id=u.id and u.produit_id=p.id  and p.status=1) as "prixmax", (select count(*) from produit_revendeur  where produit_id=p.id and p.status=1) as "offre"
                FROM  Produit p,Subcat sub,Categorie cat
                WHERE p.status=1 and ' . $criteria . ' ;';

        $query2 = 'SELECT DISTINCT s.* FROM Spec s, Produit p, Valeur v, Produit_valeur vp
                WHERE vp.product_id=p.id AND vp.valeur_id = v.id AND s.id=v.spec_id AND p.id IN 
                (SELECT p.id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and ' . $criteria . ')';

        $query3 = 'SELECT DISTINCT valeur, spec_id from Valeur v, produit_valeur pv where pv.product_id IN  (SELECT p.id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and p.subcat_id=sub.id and sub.categorie_id=cat.id and cat.nom like "' . $categorie . '") and v.id=pv.valeur_id';

        $query4 = 'SELECT DISTINCT m.nom from Marque m where m.id IN (SELECT p.marque_id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and ' . $criteria . ')';

        $query5 = 'SELECT distinct min(h.prix) as "min" ,max(h.prix) as "max" FROM Histprix h,Urlproduit u,Produit p
                    WHERE h.urlproduit_id=u.id and u.produit_id=p.id and p.status=1 AND p.id IN 
                (SELECT p.id FROM Produit p,Subcat sub, Categorie cat where p.status=1 and ' . $criteria . ')';
        $statement = $em->getConnection()->prepare($query1);
        $statement->execute();
        $produit = $statement->fetchAll();

        $statement1 = $em->getConnection()->prepare($query2);
        $statement1->execute();
        $specs = $statement1->fetchAll();

        $statement2 = $em->getConnection()->prepare($query3);
        $statement2->execute();
        $valeurs = $statement2->fetchAll();

        $statement3 = $em->getConnection()->prepare($query4);
        $statement3->execute();
        $brands = $statement3->fetchAll();

        $statement4 = $em->getConnection()->prepare($query5);
        $statement4->execute();
        $prices = $statement4->fetchAll();

        if (!$produit) {
            throw $this->createNotFoundException('The product does not exist');
        } else {
            $paginator = $this->get('knp_paginator');
            $produits = $pagination = $paginator->paginate(
                    $produit, $request->query->getInt('page', 1), $request->query->getInt('limit', 5));

            return $this->render('ProductBundle:Product:category.html.twig', array('produits' => $produits, 'specs' => $specs, 'valeurs' => $valeurs, 'brands' => $brands, 'price' => $prices,'categories'=>$categories));
        }
    }

这是第二种方法,我使用 forward 来访问生成类别的方法,而不是将其用作服务(最佳实践) .问题是如何在contactusAction()中发送categorieAction()category参数:

public function contactusAction() {
      //contactusAction()" requires that you provide a value for the "$c" argument
        $categories = $this->forward('ProductBundle:Product:categorie', array(
        'c'  => $c,
    ));
        return $this->render('ProductBundle:Default:contactus.html.twig', array('categories'=>$categories));
    }

解决方案是使用转发:

public function contactusAction() {

    $categories = $this->forward('ProductBundle:Product:categorie');
    return $this->render('ProductBundle:Default:contactus.html.twig', array('categories'=>$categories));
    }