feat: custom hx redirect class
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
@@ -58,10 +58,7 @@ trait HxTrait
|
||||
protected function htmxRedirect(Request $request, string $url): Response
|
||||
{
|
||||
if ($request->headers->has('HX-Request')) {
|
||||
$response = new Response('', Response::HTTP_OK);
|
||||
$response->headers->set('HX-Redirect', $url);
|
||||
|
||||
return $response;
|
||||
return new HxRedirectResponse($url);
|
||||
}
|
||||
|
||||
return $this->redirect($url);
|
||||
|
||||
Reference in New Issue
Block a user