Symfony UX-Turbo fetch link in Background and update

  javascript, php, symfony, turbolinks

I’m building a symfony application using Symfony UX(-Turbo). The UX-Turbo Bundle implements Hotwired’s Turbo. After following the steps on the Github page its working fine with forms but links are still loaded normally. My situation is the following:

I have a table with delete links on every row. This link points to a controller-route which deletes the specific object and then redirects the user back to the detail page. This is the code of my controller route function:

  #[Route('/client/remove-association/{id}/{csrf}', name: 'client_remove_association')]
public function removeAssociation(Request $request, ClientUserAssociation $clientUserAssociation, string $csrf, EntityManagerInterface $entityManager): Response
{
    if(!$this->isCsrfTokenValid('remove-user-association', $csrf)){
        throw new AccessDeniedHttpException();
    }
    $entityManager->remove($clientUserAssociation);
    $entityManager->flush();
    $this->addFlash('success', sprintf('%s ist nun kein %s mehr bei %s', $clientUserAssociation->getUser(), $clientUserAssociation->getRole(), $clientUserAssociation->getClient()));
    if (TurboStreamResponse::STREAM_FORMAT === $request->getPreferredFormat()) {
        return $this->render('client/list_types/_table_association_body.stream.html.twig', ["client" => $clientUserAssociation->getClient()], new TurboStreamResponse());
    }
    return $this->redirectToRoute('client_edit', ['id' => $clientUserAssociation->getClient()->getId()]);

}

If I click the link, Turbo should fetch it in the background and update the turbo-streams but its normally surf to the link and then redirect me like normal, no turbo, behaviour.

Is there any attribute or something that I have to add to the A-Tag to achieve this behaviour?

Source: Symfony Questions

LEAVE A COMMENT