我的网站上有购物车功能。
我在两个不同的页面(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)