This library provides simple way to access for request data.
composer require sivka/request
// $_POST = ['id' => 1, 'name' => 'Valera']
use Sivka\Request;
echo Request::post('name'); // Valera
$post = Request::post();
echo $post->name; // Valera
echo $post->not_defined_var; // NULL
Request::get(); // $_GET
Request::post(); // $_POST
Request::files(); // $_FILES
Request::session(); // $_SESSION
Request::cookie(); // $_COOKIE
Request::server(); // $_SERVER
Request::headers(); // http headers
All methods has same signature. Every method returns request object. If method called with argument, it returns value of specified key or NULL if key does not exists.
Sets value for specified key
$post = Request::post();
$post->set('id', 2);
// or directly
$post->id = 2;
// array maybe used
$newData = ['surname' => 'Smith', 'age' => 33];
$post->set($newData);
Returns value converted to integer or 0
echo $post->int('id'); // 2
Returns value converted to string or empty string
echo $post->string('id'); // '2'
Returns value if exists or null.
echo $post->get('id'); // 2
// or simply
echo $post->id; // 2
Methods int
, string
and get
has second optional argument specified default value if key does not exists in request object
echo $post->get('notDefined', 'define_me'); // define_me
Returns count of values
$post->count();
Returns json representation of request object
$post->toJson();
Returns request object as array
$post->toArray();
Delete key from request object
$post->delete('id');
Check if key exists in request object
echo $post->has('name'); // true
Returns array of keys from request object
$post->keys();
What's profit of this? No need to use such construction:
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
This project is licensed under the MIT License