简体   繁体   English

在Symfony2中使用SyliusCartBundle将商品添加到购物车时,如何禁用重定向?

[英]How disable redirection when add item to cart with SyliusCartBundle in Symfony2?

I'm already implement the basic of SyliusCartBundle ( http://docs.sylius.org/en/latest/bundles/SyliusCartBundle/installation.html ) 我已经实现了SyliusCartBundle的基础( http://docs.sylius.org/en/latest/bundles/SyliusCartBundle/installation.html

but the problem is the method to add item to a cart, because the only way that I know is with 但问题是将商品添加到购物车的方法,因为我知道的唯一方法是

<a href="{{ path('sylius_cart_item_add', {'productId': product.id}) }}">Add product to cart</a>

acording to http://docs.sylius.org/en/latest/bundles/SyliusCartBundle/actions.html 根据http://docs.sylius.org/en/latest/bundles/SyliusCartBundle/actions.html

and I want to add a item without redirect the webpage. 并且我想添加一个项目而不重定向网页。

sorry if is too basic but I only start with symfony 4 day ago. 对不起,如果太基础了,但我仅在4天前开始使用symfony。

Just like @zizoujal say is with ajax. 就像@zizoujal所说的是用ajax。 But when I ask I was a beginner in MVC and I dont undestand the method. 但是当我问我是MVC的初学者时,我并不理解这种方法。

The solution is: 解决方案是:

  1. Create an action that get de information of the cart and only save it, return an json respoonse like {result: success}. 创建一个获取购物车信息并仅保存它的操作,然后返回{result:success}之类的json respoonse。
  2. in the view call with ajax this action and if return result== success do the action you want. 在使用ajax的视图调用中执行此操作,如果返回结果==成功,则执行所需的操作。

Finally I dont use Symfony2 (SyliusCartBundle) but with a little more experience in MCV I can undestand and I implment my own action (in cakephp). 最终,我不使用Symfony2(SyliusCartBundle),但对MCV有了更多的经验,我可以理解并实现自己的行动(在cakephp中)。

Not the best way to do it but it may help you: 这不是最好的方法,但是它可以帮助您:

  • Write your own controller, use yourbundle_cart_item_add and add the respective routings. 编写自己的控制器,使用yourbundle_cart_item_add并添加相应的路由。
  • Imitate what it is done in the Sylius/Bundle/CartBundle/Controller/CartItemController:addAction . 模仿在Sylius/Bundle/CartBundle/Controller/CartItemController:addAction
  • At the end, instead of redirectToCarSummary() redirect to what you want. 最后,而不是redirectToCarSummary()重定向到您想要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM