Input and Output Class
Note
This class is initialized automatically by the system, so there is no need to load it manually.
The Input and Output (IO) Class in LavaLust provides a set of helper methods to safely and conveniently access request data ($_POST, $_GET, $_COOKIE, $_SERVER) and to send customized responses (HTML or JSON) with headers and status codes.
Accessing Form and Server Data
Instead of directly using superglobals like $_POST['field'] and having to check if they exist, the IO class provides safe methods that automatically return NULL if the value is not set.
Example (without IO):
<?php
$something = isset($_POST['something']) ? $_POST['something'] : NULL;
Example (with IO):
<?php
$something = $this->io->post('something');
Main Request Methods
These methods are used to fetch data from different superglobals:
<?php
// POST data
$this->io->post('field');
$this->io->post(['field1', 'field2']);
// GET data
$this->io->get('field');
$this->io->get(['field1', 'field2']);
// COOKIE data
$this->io->cookie('field');
$this->io->cookie(['cookie1', 'cookie2']);
// SERVER data
$this->io->server('field');
$this->io->server(['SERVER_PROTOCOL', 'REQUEST_URI']);
Additional Request Methods
<?php
// Search both POST and GET (POST first)
$this->io->post_get('field');
// Search both GET and POST (GET first)
$this->io->get_post('field');
// Set a cookie
// Options: domain, path, prefix, secure, samesite
$this->io->set_cookie($name, $value, $expiration, $options = []);
// Get user IP address (returns '0.0.0.0' if invalid)
$this->io->ip_address();
// Validate an IP address
$this->io->valid_ip('127.0.0.1');
// Check if request is AJAX
$this->io->is_ajax();
// Get the request method (GET, POST, etc.)
$this->io->method(); // Can be upper or lower case
Response Handling
The IO class also allows you to build and send responses with headers, content, and status codes.
Example: HTML Response
<?php
$this->io->add_header('Content-Type', 'text/html');
$this->io->set_content('Hello, World!');
$this->io->set_status_code(200);
$this->io->send();
Shorthand:
<?php
$this->io->set_html_content('Hello, World!');
$this->io->set_status_code(200);
$this->io->send();
Example: JSON Response
<?php
$this->io->add_header([
'Access-Control-Allow-Origin' => '*',
'Content-Type' => 'application/json'
]);
$data = ['message' => 'Hello, World!'];
$this->io->set_status_code(200);
$this->io->send_json($data);
Available Methods Reference
Method |
Description |
|---|---|
|
Get value from |
|
Get value from |
|
Get value from |
|
Get value from |
|
Get value from POST or GET (POST first) |
|
Get value from GET or POST (GET first) |
|
Set a cookie with additional options |
|
Returns the current user’s IP address |
|
Validate if the given IP is valid |
|
Check if the request is AJAX |
|
Return the current request method (GET/POST/etc.) |
|
Add response headers |
|
Set response content |
|
Shorthand to set HTML content |
|
Set HTTP response status code |
|
Send the response |
|
Send a JSON response |