lisennk/request

Allow to get data both from HTTP requests (e.g. $_POST, $_GET) and CLI without changing code

v1.0 2016-10-15 21:02 UTC

This package is not auto-updated.

Last update: 2024-12-14 14:37:25 UTC


README

Latest Stable Version License Build Status

One Request Class To Rule Them All.

Lisennk\Request allow to get data both from HTTP requests (e.g. $_POST, $_GET) and CLI without changing code. For exmaple, you have a script love.php. You can open it in your browser (e.g. http://localhost/love.php?name=Alice) or run via console (php love.php --name="Alice") — with Lisennk\Request you can read passed data, both ?name=Alice and --name="Alice" in the same way, just like this:

echo Request::input('name'); // outputs "Alice"

🌚 Possibilities and usage

Following the example above, we can also:

<?php

use Lisennk\Request;

$request = Request::instance();
echo $request->input('name'); // outputs "Alice"

Or you can do it like this:

echo $request->name; // outputs "Alice"

You can use all methods like in Laravel Facades, via static call:

echo Request::input('name'); // outputs "Alice"

You can use placeholder (i.e. default value) that will be returned if input doesnt exist:

echo Request::input('name', 'Unknown Name'); // outputs "Unknown Name" if "name" doesnt passed
echo $request->input('name', 'Unknown Name'); // the same same, outputs "Unknown Name" if "name" doesnt passed

You can use has method to check if value exist:

if (Request::has('name')) echo 'Name passed!'; // outputs "Name passed!" if there is value with "name" key

You can use second parameter of "has" method to check if value exist and that its equal to needed

if ($request->has('name', 'Alice')) echo 'The name is Alice!'; // outputs "the name is Alice"
if (Request::has('name', 'Bob')) echo 'Bob is here!'; // we know that name is "Alice", not "Bob", so here we will not get any output

You can use notEmpty method to check if there is any data passed to the script

if ($request->notEmpty()) echo 'There is something to work with!';

:squirrel: Installation

You can install it with Composer:

composer require lisennk/request

🌜 Contributions

Feel free to create issues and pull requests. Please, star repository, if you like it.