-
Notifications
You must be signed in to change notification settings - Fork 4
Http
← Routing Component | Template Component →
The Http
component is one of the core components of Slytherin to handle HTTP variables in a nice abstraction. Although the PHP has provided built-in functions specifically for handling HTTP, it is recommended to use its object-oriented abstractions through the PSR-07 standard to provide common interfaces for representing HTTP messages.
Slytherin has already a built-in implementation of the Http
component. The Slytherin core requires instances that implements in ServerRequestInterface
and ResponseInterface
respectively from the said PSR-07 standard:
use Rougin\Slytherin\Http\ServerRequest;
use Rougin\Slytherin\Http\Response;
/** @var \Psr\Http\Message\ServerRequestInterface */
$request = new ServerRequest($_SERVER, $_COOKIE, $_GET, $_FILES, $_POST);
/** @var \Psr\Http\Message\ResponseInterface */
$response = new Response(http_response_code());
But if there is a preferred third-party to be used, the said package should be implemented in PSR-07 standard with the required interfaces:
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
// Zend Diactoros is a PSR-07 compliant package ---
/** @var \Psr\Http\Message\ServerRequestInterface */
$request = ServerRequestFactory::fromGlobals();
/** @var \Psr\Http\Message\ResponseInterface */
$response = new Response;
// ------------------------------------------------
Once the required instances were instantiated, these must be included into a Container to be added in the Application
instance:
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Container\Container;
$container = new Container;
// ...
// Include the required instances to the container ------
$container->set(ServerRequestInterface::class, $request);
$container->set(ResponseInterface::class, $response);
// ------------------------------------------------------
(new Application($container))->run();
If using the Application
class, the HttpIntegration
can be used for creating the required HTTP request and response instances without interacting to its respective classes:
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Configuration;
use Rougin\Slytherin\Http\HttpIntegration;
$config = new Configuration;
// Set the HTTP variables to the config ---
$config->set('app.http.cookies', $_COOKIE);
$config->set('app.http.files', $_FILES);
$config->set('app.http.get', $_GET);
$config->set('app.http.post', $_POST);
$config->set('app.http.server', $_SERVER);
// ----------------------------------------
// Add the integration the run the app ---
$items = array(new HttpIntegration);
$app = new Application;
echo $app->integrate($items)->run();
// ---------------------------------------
Note
The respective instance that will be created for the said integration is only limited for the third-party packages that are currently supported by Slytherin. The built-in implementation will be used if no third-party packages are installed for the said component.
Although the Http
component has its own implementation of the PSR-07 standard through HttpIntegration
, Slytherin supports the following third-party packages below that can be integrated seamlessly to Slytherin core once their respective packages were installed:
-
Zend Diactoros package by the Zend Framework (
zendframework/zend-diactoros
)
Note
If one of the supported third-party packages was installed, their current implementations will be included by default to the container to be used by the Application
instance.
If not using the supported packages above, a list of packages that implements the PSR-07 standard are also listed at the awesome-php repository. Once selected a specified package, it must be defined in ContainerInterface
with the interfaces Psr\Http\Message\ServerRequestInterface
and Psr\Http\Message\ResponseInterface
to use it to the Slytherin core.