【问题标题】:How can I output the Cart item number?如何输出购物车商品编号?
【发布时间】:2021-06-09 01:13:42
【问题描述】:

我想知道如何显示购物车中有多少商品?我在树枝上做了一条路径,但我不知道如何展示它。

这是我的控制器,我有路线,在树枝中我用名称 (count_panier) 调用路径。

/**
    * @Route("/count/{qtt}", name="count_panier")
    */
    public function testAction($qtt,Request $req)
    {
       
       $qtt = $this->afficherCount($req);

       
        
      return $this->redirectToRoute('mag',['count'=>$qtt]);
                                                        
    }

//----------------------------------------------

    public function afficherCount(Request $req){

      $sess = $req->getSession();

      $panier = $sess->get('panier',[]);

      $qtt = 0;

      foreach($panier as $item)
      {
        $qtt += $item->quantiteCommandee;
      }

    
    
      return $qtt;


    }

这是我的树枝,这是顶部导航栏的一部分

    <div class="menu">
        <a class="active" href="{{path('mag')}}">Catalogue</a>
        <a href="contact">Contact</a>
        <a href="#creeCompte">Crée un compte</a>
        <a href="#connexion">Connexion</a>
        <a href="panier">Panier
        <img width="30" alt="img" src="{{asset('images/cart.png')}}"/></a>

        <span id='panierCompteur'>
        
         <a href="{{ path('count_panier', {'qtt': 0}) }}"></a> 

         items</span>
    </div>

【问题讨论】:

标签: php symfony twig


【解决方案1】:

在您的控制器中,您只传递了一个参数count =&gt; '$qtt

所以在 Twig 文件中,如果你想得到它,这样做:

{{ count }}

因此,如果您想获得一个显示您拥有多少物品的链接,请执行以下操作:

 <span id='panierCompteur'>
        
         <a href="{{ path('count_panier'}}"> {{count}} </a> 

         items</span>

(你没有使用 $qtt 变量所以不要传递它)

/**
    * @Route("/count", name="count_panier")
    */
    public function testAction(Request $req)
    {
       
       $qtt = $this->afficherCount($req);

       
      return $this->redirectToRoute('mag',['count'=>$qtt]);                                         
    }

//----------------------------------------------

    private function afficherCount(Request $req){

      $sess = $req->getSession();

      $panier = $sess->get('panier',[]);

      $qtt = 0;

      foreach($panier as $item)
      {
        $qtt += $item->quantiteCommandee;
      }

   
      return $qtt;
    }

当然,当你第一次渲染这个主页时,你需要在索引控制器(或任何你的主控制器)中运行函数afficherCount(),然后返回主页count =&gt; '$qtt和你所有的其他参数。

您正在重定向到另一个路由,因此如果您想获取这些参数,则需要“处理”重定向:

   /**
    * @Route("/your-route/{count?}", name="mag", requirements={"count"="\d+"})
    */
    public function yourFunction(Request $req, $count)
    {
      // true if is the first time you render this page or if you don't pass the value
      if($count === null){
         $count = afficherCount($req);
      }

      return $this->Render('yourTwigFile.html.twig',['count'=>$count]);                                         
    }

{count?} : ? if 为可选参数,因此第一次渲染此页面时无需在 URL 中传递值

requirements={"count"="\d+"} : 值只能是整数

(PS.这个函数可能是你的索引)

for more information / examples

【讨论】:

  • 当我这样做时,我收到一条错误消息,提示“count”不存在
  • @Ray 最后它在我发布的更改之后工作了吗?
猜你喜欢
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 2020-01-14
  • 2015-07-26
  • 1970-01-01
  • 2019-12-09
相关资源
最近更新 更多