Fle*_*lex 2 php controller routes cart symfony
我的网站上有购物车功能。
我在两个不同的页面(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 = $this->session->get('cart', []);
if(isset($cart[$id])){
unset($cart[$id]);
}
$this->session->set('cart', $cart);
}
public function getFullCart() : array{
$cart = $this->session->get('cart', []);
$cartWithData = [];
foreach($cart as $id => $quantity){
$cartWithData[] = [
'product' => $this->productRepository->find($id),
'quantity' => $quantity
];
}
return $cartWithData;
}
public function getTotal(): float{
$total = 0;
foreach($this->getFullCart() as $item){
$total += $item['product']->getPrice() * $item['quantity'];
}
return $total;
}
Run Code Online (Sandbox Code Playgroud)
}
购物车页面的控制器:
class CartController extends AbstractController{
/**
* @Route ("/cart", name = "cart")
*/
public function cart(CartService $cartservice){
return $this->render(
'users/cart.html.twig',
[
'items' => $cartservice->getFullCart(),
'total' => $cartservice->getTotal()
]
);
}
/**
* @Route ("/cart/add/{id}", name = "add_cart")
*/
public function add($id, CartService $cartservice, Request $request){
$cartservice->add($id);
return $this->redirectToRoute('homepage');
}
/**
* @Route ("/cart/remove/{id}", name="remove_cart")
*/
public function remove($id, CartService $cartservice){
$cartservice->remove($id);
return $this->redirectToRoute('homepage');
}
/**
* @Route ("/cart/delete/{id}", name="delete_cart")
*/
public function delete($id, CartService $cartservice){
$cartservice->delete($id);
return $this->redirectToRoute('homepage');
}
Run Code Online (Sandbox Code Playgroud)
}
我还在我的主页上显示了购物车的一部分,我显示了卡的价格/名称/数量,我的主页的控制器“HomeController.php”:
/**
* @Route("/", name="homepage")
*/
public function home(ProductRepository $productRepository, CategoryRepository $categoryRepository, CartService $cartService) : Response{
$productRepository = $this->getDoctrine()->getRepository(Product::class);
$products = $productRepository->findAll();
$categories = $categoryRepository->findAll();
return $this->render(
'users/home.html.twig',
[
'products' => $products,
'categories' => $categories,
'total' => $cartService->getTotal(),
'items' => $cartService->getFullCart()
]
);
}
Run Code Online (Sandbox Code Playgroud)
我在 CartController.php 中的函数 add 上尝试了这个
/**
* @Route ("/cart/add/{id}", name = "add_cart")
*/
public function add($id, CartService $cartservice, Request $request){
$cartservice->add($id);
$uri = $request->attributes->get("_route");
return $this->redirectToRoute($uri);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,它对我说“缺少一些强制参数(“id”)来生成路由“add_cart”的 URL。” 和乘积增量 x30。我认为该函数在路线 cart/add/{id} 上循环。
更新 :
我只需要像这样获取引用者:
/**
* @Route ("/cart/add/{id}", name = "add_cart")
*/
public function add($id, CartService $cartservice, Request $request){
$cartservice->add($id);
$route = $request->headers->get('referer');
return $this->redirect($route);
}
Run Code Online (Sandbox Code Playgroud)
我重定向到上一页