I use Symfony 4.4. I have a go back button in my Twig template.
{% if previousPageboolean == false %}<a class="d-block text-decoration-none cursor px-4 pt-3" href="{{app.request.headers.get('referer')}}"><img src="{{ asset('build/images/back.svg')}}" class="height-19"></a>{%else%}<a class="d-block text-decoration-none cursor px-4 pt-3" href="{{referer}}"><img src="{{ asset('build/images/back.svg')}}" class="height-19"></a>{% endif %}
But I have a problem. It works but in this template I have 2 forms. When I click the submit button of these buttons, flash messeges apper and reload the page. So when page reload the route of the go back button change and it is the own page.
Controller
/** * @Route("/receta/{title}", name="recipe_show", methods={"GET"}) * @Route("/receta/{title}", name="recipe_show") */public function show(Recipe $recipe,RecipeRepository $recipeRepository,ScoreRepository $scoreRepository,CommentRepository $commentRepository, Request $request): Response{ $comment = new Comment(); $score = new Score(); $comment_form = $this->createForm(CommentType::class, $comment); $comment_form->handleRequest($request); $score_form = $this->createForm(ScoreType::class, $score); $score_form->handleRequest($request); $entityManager = $this->getDoctrine()->getManager(); $user = $this->getUser(); $referrer=""; $previousPageboolean =false; if ($score_form->isSubmitted() && $score_form->isValid()) { $previousPageboolean =true; $referrer = $request->get('referrer'); $score_value = $score_form->get("score")->getData(); $isScore = $scoreRepository->findBy(array('user' => $user , 'recipe' => $recipe->getId())); if($isScore == [] ){ $score->setScore($score_value); $score->setRecipe($recipe); $score->setUser($user); $entityManager->persist($score); $entityManager->flush(); $this->addFlash('success', '¡Puntuación registrada con exito!'); return $this->redirectToRoute($request->getUri(), ['referrer' => $this->generateUrl($request->attributes->get('_route'), array_merge( $this->request->atrributes->get('_route_params'), $this->request->query->all ), ), ]); }else{ $this->addFlash('danger', 'Ya existe una puntuación registrada para esta receta con este usuario.'); return $this->redirect($request->getUri()); } } if ($comment_form->isSubmitted() && $comment_form->isValid()) { $parentid = $comment_form->get("parent")->getData(); if($parentid != null){ $parent = $entityManager->getRepository(Comment::class)->find($parentid); } $comment->setVisible(1); $comment->setParent($parent ?? null); $comment->setUser($user); $comment->setRecipe($recipe); $comment->setCreatedAt(new DateTime()); $entityManager->persist($comment); $entityManager->flush(); $this->addFlash('success', '¡Comentario registrado con exito!'); return $this->redirect($request->getUri()); } $comments = $commentRepository->findCommentsByRecipe($recipe); return $this->render('recipe/show/show.html.twig', ['recipe' => $recipe,'score_form' => $score_form->createView(),'comment_form' => $comment_form->createView(),'comments' => $comments,'referrer' => $referrer,'previousPageboolean' => $previousPageboolean ]);}
It isn't a good idea to put the name of the route in the back button because the route needs the category parameter and a recipe can have more than one categories.
The recipe table haven't a category property because I have a recipes_categories table which have a M:M relationship between recipe table and category table.