代码之家  ›  专栏  ›  技术社区  ›  Salustiano Muniz

为什么martyshka/ShoppingCart ZF2组件不工作(可能是隐藏器问题)?

  •  0
  • Salustiano Muniz  · 技术社区  · 8 年前

    我在尝试实现martyshka/ShoppingCart组件时感到非常头痛。我发现添加物品时水合器是空的,但当我强制它(在组件上设置它)时,它也不起作用。

    Here's my controller

    <?php
    namespace Publico\Controller;
    
    use Doctrine\ORM\EntityManager;
    use ShoppingCart\Controller\Plugin\ShoppingCart;
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    
    class CarrinhoController extends AbstractActionController
    {
        private $carrinho;
        private $entityManager;
    
        protected function setCarrinho(ShoppingCart $cart)
        {
            $this->carrinho = $cart;
            return $this;
        }
    
        protected function getCarrinho()
        {
            if (null === $this->carrinho) {
                $this->setCarrinho(new ShoppingCart());
            }
            return $this->carrinho;
        }
    
        /*...*/
    
        public function indexAction()
        {
            try {
                $carrinho = $this->getCarrinho();
            } catch (\Exception $e) {
                die($e->getMessage());
            }
    
            $carrinhoItems = [
                'carrinho' => $carrinho->cart(),
                'valorTotal' => $carrinho->total_sum(),
                'qtdTotal' => $carrinho->total_items(),
            ];
    
            die($carrinhoItems);
    
            return new ViewModel([
                'carrinho' => $this->carrinho->cart(),
                'valorTotal' => $this->carrinho->total_sum(),
                'qtdTotal' => $this->carrinho->total_items(),
            ]);
        }
        /*...*/
    }
    

    Here's the component repo

    1 回复  |  直到 8 年前
        1
  •  1
  •   Gautam Rai    8 年前

    这个组件提供了一个插件“ShoppingCart”来使用,你不需要创建ShoppingCart的对象。

    在行动中使用它-

    $this->ShoppingCart()