The HTTP module provides the required interfaces for handling the HTTP communication.
This interface contains the required funcionalities for handling an HTTP request message.
Get properties of an HTTP request message.
use RHo\Http\Request;
function example(RequestInterface $request): void {
var_dump($request->isHttpsScheme());
var_dump($request->method());
var_dump($request->uri());
}
If the below HTTP request was received by server:
GET /new-users/ HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: ela.do
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
By calling the example
function, it will print out the following answer.
This interface contains the required funcionalities for sending an HTTP response message.
Builds and sends an HTTP response message.
use RHo\Http\Response;
function replyWithBadRequest(ResponseInterfaceBuilder $builder): void {
$builder->init(StatusCode::BadRequest)
->withHeader('Content-Type', 'application/prs.api.ela.do+json;version=1') // optional
->withBody('{ "apple": "tree" }') // optional
->build()
->send();
}
By calling the replyWithBadRequest
function, it will send the below HTTP response message.
HTTP/1.1 400 Bad Request
Content-Type: application/prs.api.ela.do+json;version=1
Content-Length: 19
{ "apple": "tree" }
This interface contains the required funcionalities for throwing an exception with an HTTP response message.
use RHo\Http;
try {
// HTTP handling
}
catch (ExceptionInterface $ex) {
$ex->response()->send();
exit(-1);
}
The router interface helps to find the right controller for a given user request.
use RHo\Http;
$router = Router::buildWithRoutingTable([
'/new-users' => NewUserCtrl::class,
'/users' => UserCtrl::class,
'/' => RootCtrl::class
]);
$ctrl = Controller::build($router);
}
The controller interface is the main handler of the HTTP request and response messages. The below example shows an end to end handling of such communication.
use RHo\Http;
function main(ControllerInterface $ctrl): void {
try {
do {
$ctrl->validateRequest();
} while ($ctrl->nextController());
$ctrl->runService();
$ctrl->sendResponse();
}
catch (ExceptionInterface $ex) {
$ex->response()->send();
exit(-1);
}
}