33 lines
939 B
PHP
33 lines
939 B
PHP
<?php
|
|
|
|
namespace App\Htmx;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class HxRedirectResponse extends Response
|
|
{
|
|
/**
|
|
* Creates an HTMX redirect response.
|
|
*
|
|
* Constructs a response that instructs the HTMX client to navigate to the
|
|
* specified URL. The response includes the HX-Redirect header with the target
|
|
* URL. Optionally includes HX-Retarget header if a different target element
|
|
* is specified for the response content.
|
|
*
|
|
* @param string $url The URL to redirect to
|
|
* @param string|null $retarget Optional CSS selector for the target element
|
|
*/
|
|
public function __construct(string $url, ?string $retarget = null)
|
|
{
|
|
$headers = [
|
|
'HX-Redirect' => $url,
|
|
];
|
|
|
|
if (null !== $retarget) {
|
|
$headers['HX-Retarget'] = $retarget;
|
|
}
|
|
|
|
return parent::__construct(null, Response::HTTP_OK, $headers);
|
|
}
|
|
}
|