简体   繁体   English

Symfony5 上的 redirectToRoute 上一页

[英]redirectToRoute Previous page on Symfony5

I have a cart functionnality on my website.我的网站上有购物车功能。
I display the cart on two different page (cart.html.twig and home.html.twig) and I can modify the cart on those 2 pages.我在两个不同的页面(cart.html.twig 和 home.html.twig)上显示购物车,我可以在这两个页面上修改购物车。
When I'm suppose to increment a product+1 with the button I'm redirected to A SINGLE ROUTE.当我想用按钮增加一个产品+1 时,我被重定向到一个单一的路线。
If the user increment the product on the homepage I want to him to be redirected to the homepage , and if he increment the product on the cart page I want to him to be redirected to the cart page , how im suppose to do this, I'm little bit lost.如果用户在主页上增加产品,我希望他被重定向到主页如果他在购物车页面上增加产品,我希望他被重定向到购物车页面,我想怎么做,我我有点失落。
There is a way to redirect to the previous page ?有没有办法重定向到上一页?

There is the code :有代码:

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;
}

} }

THE CONTROLLER of cartpage:购物车页面的控制器:

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');
}

} }

And I also display a part of the cart on my homepage, I display the price / name / quantity of the card, the controller of my homepage "HomeController.php":而且我在我的主页上也显示了购物车的一部分,我显示了卡的价格/名称/数量,我的主页控制器“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()
        ]
    );
}

I tried this on the function add in CartController.php我在 CartController.php 中的函数添加上尝试了这个

    /**
 * @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);
}

but it doesnt work, it say to me "Some mandatory parameters are missing ("id") to generate a URL for route "add_cart"."但它不起作用,它对我说“缺少一些必需参数(“id”)来生成路由“add_cart”的 URL。 and the product increment x30.和乘积增量 x30。 I think the function loop on the route cart/add/{id}.我认为路由cart/add/{id} 上的函数循环。

UPDATE :更新 :

I just need to get the referer like this :我只需要像这样获得推荐人:

    /**
 * @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);
}

and i get redirect to the previous page然后我重定向到上一页

in your controller在你的控制器中

 /**
 * @Route ("/cart/add/{id}/{origin}", name = "add_cart")
   */
  public function add($id, $origin, CartService $cartservice, Request 
      $request){
     $cartservice->add($id);

     return $this->redirectToRoute($origin);

    }   

in your twig home page在你的树枝主页

<a href="{{ path('add_cart', 
 {'id':product.id,'origin':app.request.attributes.get('_route')})}}" >

  add +
 </a>

do the same for the others pages that you want对你想要的其他页面做同样的事情

you can get the current path in twig using您可以使用 twig 获取当前路径

app.request.attributes.get('_route') app.request.attributes.get('_route')

then you give it to your controller然后你把它交给你的控制器

and finaly you can use an if condition to redirect最后你可以使用 if 条件来重定向

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

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