Skip to content

Commit 2ca4ece

Browse files
committed
feat: support is('ajax')
1 parent 97c539c commit 2ca4ece

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

system/HTTP/IncomingRequest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa
378378
/**
379379
* Checks if this request is.
380380
*
381-
* @param string $value HTTP verb or 'json'
382-
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json' $value
381+
* @param string $value HTTP verb or 'json' or 'ajax'
382+
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $value
383383
*/
384384
public function is(string $value): bool
385385
{
@@ -395,6 +395,10 @@ public function is(string $value): bool
395395
return strpos($this->getHeaderLine('Content-Type'), 'application/json') !== false;
396396
}
397397

398+
if ($value === 'ajax') {
399+
return $this->isAJAX();
400+
}
401+
398402
throw new InvalidArgumentException('Unknown value: ' . $value);
399403
}
400404

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,13 @@ public function testIsJson()
667667
$this->assertTrue($request->is('json'));
668668
}
669669

670+
public function testIsWithAjax()
671+
{
672+
$request = $this->request->setHeader('X-Requested-With', 'XMLHttpRequest');
673+
674+
$this->assertTrue($request->is('ajax'));
675+
}
676+
670677
public function testIsCLI()
671678
{
672679
$this->assertFalse($this->request->isCLI());

0 commit comments

Comments
 (0)