feat: custom hx redirect class

This commit is contained in:
Björn Fromme
2026-03-16 12:02:27 +01:00
parent 6a945e6a08
commit 9b88a2c14d
3 changed files with 32 additions and 7 deletions
+22
View File
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Htmx;
use Symfony\Component\HttpFoundation\Response;
/**
* Response that triggers a full page navigation in HTMX.
*
* Returns a 200 status with the HX-Redirect header, causing HTMX to perform
* a client-side redirect instead of swapping content. Useful when redirecting
* between different layouts (e.g., frontend to admin area).
*/
class HxRedirectResponse extends Response
{
public function __construct(string $url)
{
parent::__construct('', Response::HTTP_OK, ['HX-Redirect' => $url]);
}
}
+1 -4
View File
@@ -58,10 +58,7 @@ trait HxTrait
protected function htmxRedirect(Request $request, string $url): Response protected function htmxRedirect(Request $request, string $url): Response
{ {
if ($request->headers->has('HX-Request')) { if ($request->headers->has('HX-Request')) {
$response = new Response('', Response::HTTP_OK); return new HxRedirectResponse($url);
$response->headers->set('HX-Redirect', $url);
return $response;
} }
return $this->redirect($url); return $this->redirect($url);
+9 -3
View File
@@ -8,6 +8,7 @@ use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\PersonalData; use App\BusProNet\Model\PersonalData;
use App\Entity\User; use App\Entity\User;
use App\Htmx\HxRedirectResponse;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -119,10 +120,15 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
'email' => $token->getUserIdentifier(), 'email' => $token->getUserIdentifier(),
]); ]);
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) { $targetPath = $this->getTargetPath($request->getSession(), $firewallName)
return new RedirectResponse($targetPath); ?? $this->urlGenerator->generate('app_account');
// When redirecting to admin from an HTMX request, force a full page navigation
// to avoid layout issues between frontend (hx-boost) and EasyAdmin
if ($request->headers->has('HX-Request') && str_contains($targetPath, '/admin')) {
return new HxRedirectResponse($targetPath);
} }
return new RedirectResponse($this->urlGenerator->generate('app_account')); return new RedirectResponse($targetPath);
} }
} }