Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit f4ba235

Browse files
authored
Merge pull request #38 from modulusphp/feature/form-request-update
Feature/form request update
2 parents 393c458 + 03cf23f commit f4ba235

4 files changed

Lines changed: 107 additions & 98 deletions

File tree

FormRequest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Modulus\Http;
4+
5+
use Modulus\Http\Request\Base;
6+
use Modulus\Http\Request\HasInput;
7+
use Modulus\Http\Request\HasRequest;
8+
use Modulus\Http\Request\HasValidation;
9+
10+
class FormRequest extends Base
11+
{
12+
use HasInput;
13+
use HasRequest;
14+
use HasValidation;
15+
}

Request.php

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Modulus\Http;
44

5-
use Modulus\Request\Server;
6-
use Modulus\Request\Cookies;
7-
use Modulus\Request\Headers;
85
use Modulus\Http\Request\Base;
96
use Modulus\Http\Request\HasInput;
107
use Modulus\Http\Request\HasRequest;
@@ -15,98 +12,4 @@ final class Request extends Base
1512
use HasInput;
1613
use HasRequest;
1714
use HasValidation;
18-
19-
/**
20-
* Construct
21-
*
22-
* @param array $data
23-
*/
24-
public function __construct(array $data = [])
25-
{
26-
$this->data = $data;
27-
28-
if (
29-
isset(getallheaders()['Content-Type']) &&
30-
(
31-
str_contains(strtolower(getallheaders()['Content-Type']), 'json') ||
32-
str_contains(strtolower(getallheaders()['Content-Type']), 'javascript')
33-
)
34-
) {
35-
$json = json_decode(file_get_contents("php://input"), true);
36-
$this->data = array_merge($this->data, is_array($json) ? $json : []);
37-
}
38-
39-
if ($this->data !== []) {
40-
foreach ($this->data as $key => $value) {
41-
if (!in_array($key, $this->protected)) $this->{$key} = $value;
42-
}
43-
}
44-
45-
$files = array_filter($data, function($file) {
46-
if (isset($file['type']) && isset($file['name']) && isset($file['size'])) return $file;
47-
});
48-
49-
$this->files = $files;
50-
$this->cookies = new Cookies();
51-
$this->headers = new Headers();
52-
$this->server = new Server();
53-
$this->path = (str_contains($_SERVER['REQUEST_URI'], '?')) ? explode('?', ($_SERVER['REQUEST_URI']))[0] : $_SERVER['REQUEST_URI'];
54-
$this->url = $_SERVER['REQUEST_URI'];
55-
$this->method = $_SERVER['REQUEST_METHOD'];
56-
$this->isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
57-
($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
58-
59-
if ($this->rules == [] && count(is_array($this->rules()) ? $this->rules() : []) > 0) {
60-
$this->rules = $this->rules();
61-
}
62-
63-
/**
64-
* Run the validate method if request has rules, and
65-
* the csrf token is present.
66-
*/
67-
if (
68-
count(is_array($this->rules) ? $this->rules : []) > 0 &&
69-
($this->has('csrf_token') || $this->headers->has('X-CSRF-TOKEN'))
70-
) {
71-
$this->validate();
72-
}
73-
}
74-
75-
/**
76-
* Get client ip address
77-
*
78-
* @return void
79-
*/
80-
public function ip($return_type = null, $ip_addresses = [])
81-
{
82-
$ip_elements = array(
83-
'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
84-
'HTTP_X_FORWARDED', 'HTTP_FORWARDED',
85-
'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_CLUSTER_CLIENT_IP',
86-
'HTTP_X_CLIENT_IP', 'HTTP_CLIENT_IP',
87-
'REMOTE_ADDR'
88-
);
89-
90-
foreach ( $ip_elements as $element ) {
91-
if(isset($_SERVER[$element])) {
92-
if ( !is_string($_SERVER[$element]) ) {
93-
// Log the value somehow, to improve the script!
94-
continue;
95-
}
96-
$address_list = explode(',', $_SERVER[$element]);
97-
$address_list = array_map('trim', $address_list);
98-
// Not using array_merge in order to preserve order
99-
foreach ( $address_list as $x ) {
100-
$ip_addresses[] = $x;
101-
}
102-
}
103-
}
104-
if (count($ip_addresses) == 0) {
105-
return false;
106-
} elseif ($return_type === 'array') {
107-
return $ip_addresses;
108-
} elseif ($return_type === 'single' || $return_type === null) {
109-
return $ip_addresses[0];
110-
}
111-
}
11215
}

Request/Base.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Modulus\Http\Request;
44

5+
use Modulus\Request\Server;
6+
use Modulus\Request\Cookies;
7+
use Modulus\Request\Headers;
58
use Modulus\Support\Extendable;
69

710
class Base
@@ -101,4 +104,92 @@ class Base
101104
'path',
102105
'url'
103106
];
107+
108+
/**
109+
* Create request instance
110+
*
111+
* @param array|null $data
112+
*/
113+
public function __construct(?array $data = [])
114+
{
115+
$this->data = $data = array_merge($_POST, $_FILES);;
116+
117+
if (
118+
isset(getallheaders()['Content-Type']) &&
119+
(
120+
str_contains(strtolower(getallheaders()['Content-Type']), 'json') ||
121+
str_contains(strtolower(getallheaders()['Content-Type']), 'javascript')
122+
)
123+
) {
124+
$json = json_decode(file_get_contents("php://input"), true);
125+
$this->data = array_merge($this->data, is_array($json) ? $json : []);
126+
}
127+
128+
if ($this->data !== []) {
129+
foreach ($this->data as $key => $value) {
130+
if (!in_array($key, $this->protected)) $this->{$key} = $value;
131+
}
132+
}
133+
134+
$files = array_filter($data, function($file) {
135+
if (isset($file['type']) && isset($file['name']) && isset($file['size'])) return $file;
136+
});
137+
138+
$this->files = $files;
139+
$this->cookies = new Cookies();
140+
$this->headers = new Headers();
141+
$this->server = new Server();
142+
$this->path = (str_contains($_SERVER['REQUEST_URI'], '?')) ? explode('?', ($_SERVER['REQUEST_URI']))[0] : $_SERVER['REQUEST_URI'];
143+
$this->url = $_SERVER['REQUEST_URI'];
144+
$this->method = $_SERVER['REQUEST_METHOD'];
145+
$this->isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
146+
($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
147+
148+
if ($this->rules == [] && count(is_array($this->rules()) ? $this->rules() : []) > 0) {
149+
$this->rules = $this->rules();
150+
}
151+
152+
/**
153+
* Run the validate method if request has rules
154+
*/
155+
if (count(is_array($this->rules) ? $this->rules : [])) $this->validate();
156+
}
157+
158+
/**
159+
* Get client ip address
160+
*
161+
* @return void
162+
*/
163+
public function ip($return_type = null, $ip_addresses = [])
164+
{
165+
$ip_elements = array(
166+
'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
167+
'HTTP_X_FORWARDED', 'HTTP_FORWARDED',
168+
'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_CLUSTER_CLIENT_IP',
169+
'HTTP_X_CLIENT_IP', 'HTTP_CLIENT_IP',
170+
'REMOTE_ADDR'
171+
);
172+
173+
foreach ( $ip_elements as $element ) {
174+
if(isset($_SERVER[$element])) {
175+
if ( !is_string($_SERVER[$element]) ) {
176+
// Log the value somehow, to improve the script!
177+
continue;
178+
}
179+
$address_list = explode(',', $_SERVER[$element]);
180+
$address_list = array_map('trim', $address_list);
181+
// Not using array_merge in order to preserve order
182+
foreach ( $address_list as $x ) {
183+
$ip_addresses[] = $x;
184+
}
185+
}
186+
}
187+
if (count($ip_addresses) == 0) {
188+
return false;
189+
} elseif ($return_type === 'array') {
190+
return $ip_addresses;
191+
} elseif ($return_type === 'single' || $return_type === null) {
192+
return $ip_addresses[0];
193+
}
194+
}
104195
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "modulusphp/http",
33
"description": "Http component for Modulus",
4-
"version": "1.9.6.2",
4+
"version": "1.9.6.3",
55
"license": "MIT",
66
"type": "package",
77
"authors": [{

0 commit comments

Comments
 (0)