feat: improved error handling and redirect on invalid session
This commit is contained in:
+21
-1
@@ -4,10 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Htmx;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Trait providing HTMX Out-of-Band swap functionality for controllers.
|
||||
* Trait providing HTMX functionality for controllers.
|
||||
*/
|
||||
trait HxTrait
|
||||
{
|
||||
@@ -46,4 +47,23 @@ trait HxTrait
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a redirect response that works correctly with HTMX requests.
|
||||
*
|
||||
* For HTMX requests, returns a 200 response with HX-Redirect header,
|
||||
* which triggers a full page navigation (avoiding CORS issues with cross-origin redirects).
|
||||
* For regular requests, returns a standard HTTP redirect.
|
||||
*/
|
||||
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 $this->redirect($url);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user