HTTP package for the MODULEWork Framework
The HTTP Package of the Modulework Framework., (*2)
It provides a convient way of handling HTTP request and HTTP response., (*3)
So for example you could already create a application with these two classes:, (*4)
$req = Request::makeFromGlobals(); $content = 'Hello ' . $req->query->get('name', 'Stranger'); $res = Response::make($content); $res->send();
This will app will great every vistor with their name or if the vistor didn' t provide the name in the query string it will fallback to "Stranger"., (*5)
Of course this is a very basic example but it can do a lot more!, (*6)
We could expand this and save the name into a cookie:, (*7)
$req = Request::makeFromGlobals(); $name = $req->query->get('name', Stranger); $content = 'Hello ' . $name; $res = Response::make($content); $res->addCookie(Cookie::make( 'name', $name )); $res->send();
Or check if the method is GET or POST, so a user could also POST it' s name:, (*8)
$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();
Or display the client' s IP:, (*9)
echo Request::makeFromGlobals()->getClientIp();
Now we also send a cookie a too the user. But we can do even more:, (*10)
$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();
Chained methods! Custom Headers! Custom Status Codes! And much more!, (*11)
The Response class is intelligent enough to set the status code, if a redirect is issued:, (*12)
$res = Response::make() ->addHeader('Location', 'foo.bar') ->prepare($request) ->send()
Will result in this header, (*13)
HTTP/1.0 302 Found Location: foo.bar Date [...]
But the Request class can do even more, we can use it for very basic routing:, (*14)
$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!" }
This is very basic and not best practice, but it shows for what we can use this class for!, (*15)
There are also some more Response classes, like the RedirectResponse and JsonResponse class., (*16)
Here is an usage example:, (*17)
$res = RedirectResponse::make('http://foo.bar') ->withCookie(Cookie::make('foo')) ->prepare(Request::makeFromGlobals) ->send();
As you can see very easy to use. NOTE! This is just a wrapper for a normal Response. You could also do this:, (*18)
$res = Response::make('<extra>', 302, array('Location' => 'http://foo.bar')) ->addCookie(Cookie::make('foo')) ->prepare(Request::makeFromGlobals) ->send();
The only thing what is not shown in the alternative way is the HTML meta redirect (in case the header doesn' t fire)., (*19)
The JsonResponse class is pretty straight forward as well:, (*20)
$res = JsonResponse::make(array('foo' => 'bar')) ->prepare(Request::makeFromGlobals) ->send();
This would result in this response:, (*21)
HTTP/1.0 200 OK Content-Type application/json Date [...] {"foo": "bar"}
Pretty nifty, eeh!!?, (*22)