Skip to content

Commit 0535173

Browse files
committed
feat: add support for method overrides
1 parent b566290 commit 0535173

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/Request.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
*/
1717
class Request
1818
{
19-
const METHOD_HEAD = 'HEAD';
20-
const METHOD_GET = 'GET';
21-
const METHOD_POST = 'POST';
22-
const METHOD_PUT = 'PUT';
23-
const METHOD_PATCH = 'PATCH';
24-
const METHOD_DELETE = 'DELETE';
25-
const METHOD_OPTIONS = 'OPTIONS';
26-
const METHOD_OVERRIDE = '_METHOD';
19+
public const METHOD_HEAD = 'HEAD';
20+
public const METHOD_GET = 'GET';
21+
public const METHOD_POST = 'POST';
22+
public const METHOD_PUT = 'PUT';
23+
public const METHOD_PATCH = 'PATCH';
24+
public const METHOD_DELETE = 'DELETE';
25+
public const METHOD_OPTIONS = 'OPTIONS';
26+
public const METHOD_OVERRIDE = '_METHOD';
2727

2828
protected static $errors = [];
2929
protected static $formDataMediaTypes = ['application/x-www-form-urlencoded'];
@@ -44,10 +44,39 @@ public function __construct()
4444
* @return string
4545
*/
4646
public static function getMethod(): string
47+
{
48+
if ($method = static::methodOverride()) {
49+
return $method;
50+
}
51+
52+
return static::getOriginalMethod();
53+
}
54+
55+
/**
56+
* Get original method
57+
*/
58+
public static function getOriginalMethod(): string
4759
{
4860
return $_SERVER['REQUEST_METHOD'];
4961
}
5062

63+
/**
64+
* Is this a method override request?
65+
* @return string|null
66+
*/
67+
public static function methodOverride()
68+
{
69+
if ($method = static::headers('X-Http-Method-Override')) {
70+
return strtoupper($method);
71+
}
72+
73+
if (static::getOriginalMethod() === 'POST') {
74+
return strtoupper(static::get(static::METHOD_OVERRIDE));
75+
}
76+
77+
return null;
78+
}
79+
5180
/**
5281
* Check for request method type
5382
*
@@ -468,7 +497,7 @@ public static function upload(string $key, string $destination, array $config =
468497
public static function uploadAs(string $key, string $destination, string $name, array $config = [])
469498
{
470499
$fileExtension = pathinfo($_FILES[$key]['name'], PATHINFO_EXTENSION);
471-
500+
472501
$config['rename'] = true;
473502
$config['name'] = $name . '.' . $fileExtension;
474503

0 commit comments

Comments
 (0)