Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ziadoz/884fe8853461f828bd5d to your computer and use it in GitHub Desktop.
Save ziadoz/884fe8853461f828bd5d to your computer and use it in GitHub Desktop.
Simple PHP HTTP Handling
<?php
// See Error Handling in PHP: https://nomadphp.com/2015/02/25/nomadphp-2015-02-us-lt2/#
$display = function ($status, $headers = array(), $content = '') {
http_response_code($status);
foreach ($headers as $key => $value) {
header($key . ': ' . $value);
}
echo $content;
};
$dispatch = function ($display, $response) {
return call_user_func_array($display, $response);
};
$controller = function ($get, $post, $cookie, $server) {
$content = '<p>Hello, ' . (isset($get['name']) ? $get['name'] : 'World') . '!</p>';
return array(200, array('X-Powered-By' => 'Awesome'), $content);
};
$dispatch($display, $controller($_GET, $_POST, $_COOKIE, $_SERVER));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment