PHP code example of modulework / http

1. Go to this page and download the library: Download modulework/http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

modulework / http example snippets


$req = Request::makeFromGlobals();
    
$content = 'Hello ' . $req->query->get('name', 'Stranger');
    
$res = Response::make($content);
$res->send();

$req = Request::makeFromGlobals();
    
$name = $req->query->get('name', Stranger);
$content = 'Hello ' . $name;
    
$res = Response::make($content);
$res->addCookie(Cookie::make(
		'name',
		$name
));
$res->send();

$req = Request::makeFromGlobals();

$name = $req->query->get('name',
	$req->request->get('name', 'Stranger')
	);
// Or just getting the method:
$method = $req->getMethod();

$content = 'Hello ' . $name;

$res = Response::make($content);
$res->addCookie(Cookie::make(
		'name',
		$name
		));
$res->send();

echo Request::makeFromGlobals()->getClientIp();

$res = Response::make()
->setContent('Foo')
->setStatusCode(200)
->addHeader('Expire', 'never')
->setDate(new DateTime)
->addCookie(Cookie::make('foo')
	->setValue('bar')
	->setSecure(false)
	->setHttpOnly(false)
)
->prepare($request)
->send();

$res = Response::make()
->addHeader('Location', 'foo.bar')
->prepare($request)
->send()

$req = Request::makeFromGlobals();
if ('/' == $req->getPath()) {
    echo "You are on the homepage"
} elseif ('/foo/bar' == $req->getPath()) {
    echo "You are on the bar page of foo!"
}

$res = RedirectResponse::make('http://foo.bar')
->withCookie(Cookie::make('foo'))
->prepare(Request::makeFromGlobals)
->send();

$res = Response::make('<extra>', 302, array('Location' => 'http://foo.bar'))
->addCookie(Cookie::make('foo'))
->prepare(Request::makeFromGlobals)
->send();

$res = JsonResponse::make(array('foo' => 'bar'))
->prepare(Request::makeFromGlobals)
->send();