Code Monkey home page Code Monkey logo

request's Introduction

Simple http request library

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

request's People

Contributors

alex-sivka avatar alexsivka avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.