小编Fle*_*lex的帖子

redirectToRoute Symfony5 上的上一页

我的网站上有购物车功能。
我在两个不同的页面(cart.html.twig 和 home.html.twig)上显示购物车,并且我可以修改这两个页面上的购物车。
当我想用按钮增加产品+1时,我被重定向到单一路线。
如果用户在主页上增加产品,我希望他被重定向到主页如果他在购物车页面上增加产品,我希望他被重定向到购物车页面,我想如何做到这一点,我我有点失落。
有没有办法重定向到上一页?

有代码:

class CartService {

protected $session;
protected $productRepository;

public function __construct(SessionInterface $session, ProductRepository $productRepository){
    $this->session = $session;
    $this->productRepository = $productRepository;
}

public function add(int $id){
    $cart = $this->session->get('cart', []);

    if(isset($cart[$id])){
        $cart[$id]++;
    } else {
        $cart[$id] = 1;
    }

    $this->session->set('cart', $cart);
}

public function remove(int $id) {
    $cart = $this->session->get('cart', []);

    if(isset($cart[$id])){
        if($cart[$id] > 1){
            $cart[$id]--;
        } else {
            unset($cart[$id]);
        }
    }

    $this->session->set('cart', $cart);
}

public function delete(int $id){
    $cart …
Run Code Online (Sandbox Code Playgroud)

php controller routes cart symfony

2
推荐指数
1
解决办法
8207
查看次数

标签 统计

cart ×1

controller ×1

php ×1

routes ×1

symfony ×1