Cakephp——It';不重定向到购物车项目页面";手推车/索引“;单击“添加到购物车”按钮后

Cakephp——It';不重定向到购物车项目页面";手推车/索引“;单击“添加到购物车”按钮后,php,cakephp,cakephp-3.0,cakephp-2.0,cakephp-2.3,Php,Cakephp,Cakephp 3.0,Cakephp 2.0,Cakephp 2.3,您可以在视图中看到添加单个产品的代码 当我按下添加到购物车按钮时,如下所示 这看起来像蛋糕3,而不是2或2.3,请只标记适当的版本号。它应该重定向到哪个URL?它在做什么?它是在空的情况下失败,不是在空的情况下失败,还是两者都失败?我想重定向到Carts控制器和索引页面。因此,它的购物车/索引@GregSchmidtAnd不是那样,它在做什么?你只回答了三个问题中的一个。它只是重定向到同一页@GregSchmidt,但它正在将数据保存在数据库表购物车中。 <?= $this->F

您可以在视图中看到添加单个产品的代码

当我按下添加到购物车按钮时,如下所示


这看起来像蛋糕3,而不是2或2.3,请只标记适当的版本号。它应该重定向到哪个URL?它在做什么?它是在空的情况下失败,不是在空的情况下失败,还是两者都失败?我想重定向到Carts控制器和索引页面。因此,它的购物车/索引@GregSchmidtAnd不是那样,它在做什么?你只回答了三个问题中的一个。它只是重定向到同一页@GregSchmidt,但它正在将数据保存在数据库表购物车中。
 <?= $this->Form->submit('add to cart', ['class'=>'btn_3']); ?>
<div class="single_product_text text-center">
            <h3><?= h($product->name) ?></h3>
            <p><?= h($product->description) ?></p>

            <?= $this->Form->create(null,['id'=>'cartForm', 'url'=>['controller' => 'Products', 'action' => 'addItem', $product->id]]); ?>
              <div class="card_area">
                  <div class="product_count_area">
                      <p>Quantity</p> 
                      <div class="product_count d-inline-block">
                        
                          <span class="product_count_item inumber-decrement"> <i class="ti-minus"></i></span>
                          <?= $this->Form->hidden('user_id', ['value'=>$this->request->getSession()->read('Auth.User.id')]); ?>
                          <?= $this->Form->hidden('product_id', ['value'=>$product->id]); ?>
                          <?= $this->Form->hidden('price', ['value'=>$product->sell_price]); ?>
                          <?= $this->Form->input('quantity', ['class'=>'product_count_item input-number', 'value'=>1, 'min'=>0, 'max'=>6, 'label' => false,]); ?>
                          <span class="product_count_item number-increment"> <i class="ti-plus"></i></span>
                        
                        
                      </div>
                      <p>$<?= h($product->sell_price) ?></p>
                  </div>
                <div class="add_to_cart">
                    <?= $this->Form->submit('add to cart', ['class'=>'btn_3']); ?>
                </div>
              </div>
            <?= $this->Form->end(); ?>
          </div>
        </div>
public function addItem($id = null)
    {
        $cartModel = $this->loadModel('Carts');

        $cart = $cartModel->find()
        ->where(['Carts.product_id' => $id,  'Carts.user_id' => $this->request->getData('user_id')])
        ->contain(['Products']);

        if($cart->isEmpty())
        {
            $cart = $cartModel->newEmptyEntity();
            if ($this->request->is('post')) 
            {
                $cart = $cartModel->patchEntity($cart, $this->request->getData());
                if ($cartModel->save($cart)) 
                {
                    return $this->redirect(['controller'=>'Products', 'action' => 'view', $id]);
                }
                $this->Flash->error(__('The cart could not be saved. Please, try again.'));
            }//end of post if
        }//end of isEmpty if
        else
        {
            if ($this->request->is(['patch', 'post', 'put'])) 
            { 
                foreach($cart as $cart1)
                {
                    $cart1->quantity += $this->request->getData('quantity');
                    if ($cartModel->save($cart1)) 
                    {
                 //       $this->Flash->set(__('The cart has been saved.'), ['element'=>'success']);
                        return $this->redirect(['controller'=>'Carts', 'action' => 'index']);
                    }
                    $this->Flash->set(__('The cart could not be saved. Please, try again.'), ['element'=>'error']);
                }//end of foreach
            }//end of if
        }//end of else