Goal: install the package, create an Inputs instance (or use the
facade), and read your first request value.
composer require initphp/inputRequirements: PHP 8.1+, ext-json, and the initphp/parameterbag ^2.0
and initphp/validation ^2.0 packages (pulled in automatically).
The facade builds a single backing instance from the superglobals on first use and reuses it for the rest of the request.
require_once 'vendor/autoload.php';
use InitPHP\Input\Facade\Inputs as Input;
// GET /?name=Jane
echo Input::get('name', 'John');Expected output: Jane (or John when name is absent).
use InitPHP\Input\Inputs;
$input = new Inputs(); // reads $_GET, $_POST and php://input
echo $input->get('name', 'John');new Inputs() with no arguments reads the live superglobals and request
body. To make the object self-contained — for tests, CLI tooling, or a
custom request abstraction — pass the sources in:
$input = new Inputs(
get: ['name' => 'Jane'],
post: ['email' => 'jane@example.com'],
raw: ['token' => 'abc123'],
);
echo $input->get('name'); // 'Jane'
echo $input->post('email'); // 'jane@example.com'
echo $input->raw('token'); // 'abc123'Any argument left as null falls back to its global: get to $_GET,
post to $_POST, and raw to the decoded php://input body.
Every accessor returns either the stored value or the default you
pass as the second argument (null when omitted):
$input = new Inputs(get: ['page' => '2']);
$input->get('page', 1); // '2' (note: still a string from the query)
$input->get('missing', 1); // 1 (the default)
$input->get('missing'); // nullValues are returned exactly as the source provides them. Query and form values are strings; JSON-body values keep their decoded type (
int,bool,array, …).
- Expecting type casting.
get('page', 1)returns the string'2', not an integer. Cast it yourself, or validate with a rule such as['integer']. - Assuming case-insensitive keys.
get('Name')andget('name')are different keys. See the FAQ.