使用Symfony的购物系统


Shopping system using Symfony

我正在学习symfony,我想为我的学校项目建立一个商店系统。我想将用户选择的项目列表发送到另一个我不知道如何操作的控制器。

这是我的展示项目操作

public function ShowItemAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $form = $this->createFormBuilder()
        ->add('search', 'textbox', array(
            'attr' => array(
                'label' => false
            )))->getForm();
    $form->handleRequest($request);
    if (($form->isSubmitted()) && ($form->isValid())) {
        $formdata = $request->request->get('form');
        $search = $formdata['search'];
        $products_Repo = $em->getRepository('MyShopBundle:Products')->GetProducts($search);
        return $this->render('MyShopBundle:Products:show.html.twig',array(
            'form' => $form->createView(),
            'products'=> $products_Repo
        ));
    }
    return $this->render('MyShopBundle:Products:show.html.twig',array(
        'form' => $form->createView()
    ));
}

和我的节目.html.Twig

{% block body%}
<div id="cart-content">
</div>
    <div class="cart-buttons">
    <button id="cart">View Shopping Cart</button>
    <button id="clear-cart">Clear Cart</button>
</div>
{{ form(form) }}
{% if products is defined %}
    <table>
        <thead>
        <tr>
            <th>Partner-Name</th>
            <th>ProductNr</th>
            <th>Description</th>
            <th>price</th>
        </tr>
        </thead>
        <tbody>
        {% for product in products %}
            <tr>
                <td><div class="partner">{{product.partner}}</div></td>
                <td><div class="partnerdata" data-value="{{ product.id }}">{{ product.productnr }}</div></td>
                <td><div class="description"> {{ product.description }}</div></td>
                <td><div class="price">{{ product.price }}</div></td>
                <td class="counter"><input type="number" name="count" min="1" step="1"></td>
                <td class="cart">
                    <button type='submit'>in den Warenkorb</button>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endif %}
{% endblock %}

我的JavaScript:

 $('.cart').click(function (event) {
        event.preventDefault();
        var closestTr = $(this).closest('tr');
        var ref = closestTr.find('.partner').text();
        var data_value = closestTr.find('.partnerdata').data('value');
        var productNr=    closestTr.find('.partnerdata').html();
        var price= closestTr.find('.price').html();
        var count = closestTr.find('input').val();
        if (count < 1) {
        }
        else {
            $(".cart-content").prepend(HS_ref + "|" + data_value + "|" + herstellerNr + "|" + count + "|" + vk);
        }
    });

我可以看到用户选择的数据(<div id="cart-content"></div>内(,但我不知道如何将这些内容作为 POST 发送到控制器。

像这样的东西:https://www.codeofaninja.com/2013/04/shopping-cart-in-php.html

我正在开发Symfony 2.7

您可以使用 AJAX 执行此操作

首先,您需要获取 html 数据

其次,您必须将此数据发送到控制器

第三,你必须在控制器中获取数据

这是一个例子:

首先 "你需要获取html数据">

.HTML:

<div id="cart-content" data-route="{{ path('get_user_card_items') }}">
    <span data-item-id="item1" data-quantity="2"></span>
    <span data-item-id="item2" data-quantity="1"></span>
    <span data-item-id="item3" data-quantity="5"></span>
</div>

.JS

/**
 * Get the content of #cart-content
 */
function getCartItems() {
    var items = [];
    $('#cart-content span').each(function(){ // loop into all span
        var item = { // create an item object who get all the data
            'id' : $(this).attr('data-item-id'),
            'quantity' : $(this).attr('data-quantity'),
        }
        items.push(item); // push into an array
    });
    return items;
}

第二个"你必须将此数据发送给控制器">

.JS

function sendCartItems() {
    $.ajax({
        url: $('#cart-content').attr('data-route'), // here is a route variable
        method: 'POST',
        data: {items: getCartItems()},
        success: function (data) {
            // do some stuff when it's send
        }
    });
}

第三">你必须在控制器中获取数据">

自定义控制器.php

class CustomController extends Controller
{
    /**
     * @Route("/getUserCardItems",  name="get_user_card_items")
     */
    public function getUserCardItemsAction(Request $request)
    {
        $items = $request->get('items');
        var_dump($items);die; // display for you the items to see if it's works (look into the networks console tab on google chrome)
       // some stuff like sending items to database...
    }
}

访问网络选项卡谷歌浏览器

你做到了;)