From 777ca8cd8e58bee70fae38811298af7496a2c627 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Wed, 6 Nov 2024 09:55:30 +0300 Subject: [PATCH] feat: Add in config parameter `storePreviousURL` --- src/CodeIgniter.php | 8 +++++++- src/Config/Htmx.php | 6 ++++++ tests/CodeIgniterTest.php | 23 ++++++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/CodeIgniter.php b/src/CodeIgniter.php index 3e6d1ea..be4f19f 100644 --- a/src/CodeIgniter.php +++ b/src/CodeIgniter.php @@ -8,6 +8,7 @@ use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\URI; +use Michalsn\CodeIgniterHtmx\Config\Htmx; use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest as HTMXIncomingRequest; /** @@ -41,8 +42,13 @@ public function storePreviousURL($uri) return; } + $storePreviousURL = config(Htmx::class)->storePreviousURL; + // Ignore AJAX and HTMX requests - if ((method_exists($this->request, 'isHTMX') && $this->request->isHTMX()) || (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())) { + if ( + ($storePreviousURL === false && (method_exists($this->request, 'isHTMX') && $this->request->isHTMX())) + || (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) + ) { return; } diff --git a/src/Config/Htmx.php b/src/Config/Htmx.php index fd6b4e2..fb88a95 100644 --- a/src/Config/Htmx.php +++ b/src/Config/Htmx.php @@ -22,4 +22,10 @@ class Htmx extends BaseConfig * when they are enabled. */ public string $skipViewDecoratorsString = 'htmxSkipViewDecorators'; + + /** + * Saving the previous URL between requests + * Sometimes this breaks the logic - the previous URL is part of the HTMX instead of the request page + */ + public bool $storePreviousURL = true; } diff --git a/tests/CodeIgniterTest.php b/tests/CodeIgniterTest.php index 8ae5f92..9714d71 100644 --- a/tests/CodeIgniterTest.php +++ b/tests/CodeIgniterTest.php @@ -5,6 +5,7 @@ namespace Tests; use CodeIgniter\Test\CIUnitTestCase; +use Michalsn\CodeIgniterHtmx\Config\Htmx; /** * @internal @@ -29,7 +30,7 @@ public function testIsHTMXNotSavePreviousURL(): void { // Default request behavior $uri = service('uri'); - $request = service('incomingrequest'); + $request = service('request'); $uri->setPath('/')->setQuery('previous=original'); @@ -40,14 +41,30 @@ public function testIsHTMXNotSavePreviousURL(): void $this->assertArrayHasKey('_ci_previous_url', $_SESSION); $this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']); - // HTMX request - $uri->setPath('/')->setQuery('previous=htmx'); + // HTMX request with the previous URL saved + config(Htmx::class)->storePreviousURL = true; + + $uri->setPath('/')->setQuery('previous=saved_from_htmx'); $request->appendHeader('HX-Request', 'true'); ob_start(); service('codeigniter', null, false)->setContext('web')->run(); ob_get_clean(); + $this->assertTrue($request->isHTMX()); + $this->assertArrayHasKey('_ci_previous_url', $_SESSION); + $this->assertSame('https://example.com/index.php/?previous=saved_from_htmx', $_SESSION['_ci_previous_url']); + + // HTMX request without saving the previous URL + $_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original'; + + config(Htmx::class)->storePreviousURL = false; + $uri->setPath('/')->setQuery('previous=not_saved_from_htmx'); + + ob_start(); + service('codeigniter', null, false)->setContext('web')->run(); + ob_get_clean(); + $this->assertTrue($request->isHTMX()); $this->assertArrayHasKey('_ci_previous_url', $_SESSION); $this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']);