sivka/request

v1.0.2 2019-03-24 13:16 UTC

This package is auto-updated.

Last update: 2025-04-25 09:24:17 UTC


README

This library provides simple way to access for request data.

Installation

composer require sivka/request

Table of contents

Example

// $_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

Available methods

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.

Methods of request object

set

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);

int

Returns value converted to integer or 0

echo $post->int('id'); // 2

string

Returns value converted to string or empty string

echo $post->string('id'); // '2'

get

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

count

Returns count of values

$post->count();

toJson

Returns json representation of request object

$post->toJson();

toArray

Returns request object as array

$post->toArray();

delete

Delete key from request object

$post->delete('id');

has

Check if key exists in request object

echo $post->has('name'); // true

keys

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;

License

This project is licensed under the MIT License