📥 HTTP Requests
The Request class in PageBlocks provides a simple yet powerful way to work with incoming HTTP requests. It wraps superglobal variables ($_GET, $_POST, $_FILES, $_SERVER, etc.) and offers a clean API for accessing data, headers, files, checking request type, and validation.
You can obtain a request instance in two ways:
🧩 Via Global Helper
request() // Returns the Request instance
request('email') // Immediately returns the value by keyThe request() helper is a shortcut to get the current HTTP request or a specific parameter value.
⚙️ Via Dependency Injection in Closure
use Boshnik\PageBlocks\Http\Request;
Route::get('/', function (Request $request) {
$email = $request->email;
});The Request class is automatically injected into route closures or controllers when specified in parameters.
📍 Path and URL
path() — request path without domain
request()->path(); // '/catalog/items'url() — current URL (without parameters)
request()->url(); // 'https://example.com/catalog/items'fullUrl() — full request URL
request()->fullUrl(); // 'https://example.com/catalog/items?sort=price'rootUrl() — site base URL
request()->rootUrl(); // 'https://example.com'isSecure() — whether the request is secure (HTTPS)
request()->isSecure(); // true or false📡 Request Method
method() — returns HTTP method (GET, POST, PUT...)
if (request()->method() === 'POST') {
// form processing
}isMethod() — checks request type
if (request()->isMethod('post')) {
//...
}🧾 Headers
header($key) — get a header
request()->header('Accept'); // 'text/html,application/xhtml+xml,...'hasHeader($key) — check if header exists
request()->hasHeader('Authorization');🌐 Client IP Address
request()->ip(); // e.g.: '192.168.1.10'🔍 Checking Request Type
ajax() — is this an AJAX request?
if (request()->ajax()) {
return json(['ok' => true]);
}isJson() — does the request contain JSON?
request()->isJson(); // true if JSON in Content-Type or AcceptexpectsJson() — does the client expect JSON response?
request()->expectsJson(); // true for APIaccepts(array $types) — does the client support the required type?
request()->accepts(['application/json', 'text/html']);📥 Getting Data
input($key) — get any input value (GET, POST, JSON, FILES)
request()->input('email');
request('email');get($key, $default) — same as above
request()->get('email', 'no@email.com');has($key) — does the value exist?
if (request()->has('token')) {
// ...
}💾 All Request Data
request()->all(); // returns array from query + post + json + files✉️ Getting GET and POST
request()->query('id'); // $_GET['id']
request()->post('name'); // $_POST['name']🧪 Getting JSON Data
If the request contains Content-Type: application/json, you can get:
request()->json('title'); // single value
request()->json(); // entire JSONExample:
{
"title": "Hello world",
"tags": ["php", "modx"]
}📁 Working with Files
hasFile($key) — was a file uploaded
if (request()->hasFile('avatar')) {
// ...
}file($key) — get uploaded file
$file = request()->file('avatar');
$file->store('uploads/'); // UploadedFile::store($directory, $filename)For a multi-file input,
file()returns an array ofUploadedFileobjects.
allFiles() — get all files
$all = request()->allFiles(); // array of files, keyed by input name🧪 Data Validation
$data = request()->validate([
'email' => 'required|email',
'password' => 'required|min:6',
]);If validation fails, nothing is thrown and nothing is returned: the error response is sent and the request stops right there (JSON with status 422 for AJAX, a redirect back with {$errors} and {$old_input} otherwise). So there is nothing to catch — the code after validate() only runs on valid input. See Validation.
🔮 Magic Properties
You can access data directly as properties:
$name = request()->name;
request()->city = 'Kyiv';🧰 Full Request Handling Example
public function store(Request $request)
{
// Form data validation
$validated = $request->validate([
'honeypot' => 'empty',
'pagetitle' => 'required|unique:modResource',
'alias' => 'nullable|string',
'template' => 'required|numeric|exists:modTemplate,id',
'content' => 'nullable|string',
]);
// Merge with default MODX resource values
$data = array_merge([
'class_key' => 'modDocument',
'context_key' => 'web',
'content_type' => 1,
'published' => 1,
'publishedon' => '',
'publishedby' => $this->modx->user->id ?? 1,
'deleted' => 0,
], $validated);
// Save resource
$status = query(\modResource::class)->create($data);
return $status
? response()->success('Resource created successfully')
: response()->error('Error creating resource');
}📌 Summary
| Method | Description |
|---|---|
request() or $request in controller | Get request instance |
input(), get() | Get value by key |
all() | All request data |
method(), isMethod() | Request method |
header(), hasHeader() | Headers |
ip() | Client IP |
ajax(), isJson(), expectsJson() | Request type |
json() | Request JSON body |
file(), hasFile(), allFiles() | Working with files |
validate() | Input data validation |