HTTPTools
A subset of my PHP prototypes, I use these helper static classes whenever I need to work with form data, request methods, specialized output and session operations to avoid boilerplate isset() array_key_exists() madness.
A subset of my PHP prototypes, I use these helper static classes whenever I need to work with form data, request methods, specialized output and session operations to avoid boilerplate isset() array_key_exists() madness.
Repository
https://github.com/spoonchucks/prototype-php/tree/master/HttpTools
Example Usage
Since no individual component depends on the existence of the other, each can be used on its own.
Request.php (source)
At its core, this is a wrapper for the $_REQUEST superglobal.
<?php if (Request::POST()) { // Contains value of $_POST['userId'], $_GET['userId'] if they exist or NULL if not $foo = Request::getParam('userId'); // The following lines are equivalent if (null === $foo) echo '$foo contains no value'; if (!Request::exists('userId')) echo 'No userId field set with Form data'; }
Session.php (source)
At its core, this is a wrapper for the $_SESSION superglobal.
<?php Session::put('somevar', 'Hello World'); // puts 'Hello World' into $_SESSION['somevar'] $somevar = Session::get('somevar'); // returns value of $_SESSION['somevar'] Session::erase('somevar'); // unsets $_SESSION['somevar'] // other methods Session::exists('somevar'); // returns true or false Session::wipe(); // unsets all Session variables Session::close(); // destroys the Session // debugging Session::dump(); // dumps a table of all the Session variables
Response.php (source)
This provides a simple way to output special data. It also eliminates the need to prepend <pre> tags or force Content-Type headers and exit() for dumping out complex data structures for debugging.
<?php // // USE CASE 1 -- JSON // $someObject = new User(); $someObject->firstName = 'Dave'; $someObject->favoriteFoods = ['hamburgers', 'pancakes']; $statusCode = Response::CREATED; Response::json($someObject, $statusCode); // // USE CASE 2 -- DOWNLOAD AS ATTACHMENT // Response::prepareDownload('image/jpeg', 'default_filename.jpg'); fpassthru($fp); // // DEBUGGING // $complexDataStructure = getSomethingComplex(); Response::text($complexDataStructure);








