我有一个产品列表,每个产品都有一个复选框。我想在标记它们时获得市场产品,然后单击“添加到购物车”按钮。当我尝试转储产品时,阵列中没有数据。
这是我的Twig
{% extends 'base.html.twig' %}
{% block body %}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<div style="text-align: center;">
<h2><u>Recent Products</u></h2>
<hr>
<form action="{{ path('cart_add') }}" method="post">
{% for product in products %}
<div class="row">
<div class="col-md-7">
<img src="/images/products/{{ product.image }}" alt="" width="300" height="250">
</div>
<div class="col-md-5">
<h4><b>Name: </b>{{ product.name }}</h4>
<h4><b>Description: </b></h4>
Mark It <input type="checkbox" value="{{ product.id }}" name="products[]">
<p>{{ product.description }}</p>
<a class="btn btn-primary" href="/products/details/{{ product.id }}">View Product <span class="glyphicon glyphicon-chevron-right"></span></a>
<br>
<br>
{% if is_granted('ROLE_ADMIN') or is_granted('ROLE_EDITOR') %}
<a href="/products/edit/{{ product.id }}" class="btn btn-default">Edit Product</a>
<a href="/products/delete/{{ product.id }}" class="btn btn-danger">Delete Product</a>
{% endif %}
</div>
</div>
<hr>
{% endfor %}
<a href="/cart/add" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-shopping-cart"></span> Add To Cart</a>
</form>
<div class="navigation">
{{ knp_pagination_render(products) }}
</div>
</div>
{% endblock %}
和CartController
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class CartController extends Controller
{
/**
* @Route("/cart/add", name="cart_add")
*/
public function addToCartAction(Request $request)
{
var_dump($request->get('products', []));
die();
}
/**
* @Route("/cart/show", name="cart_list")
*/
public function listCartAction()
{
return $this->render('cart/cart.view.html.twig', [
'products' => ...
]);
}
}