Skip to content
Rougin Gutib edited this page Dec 22, 2023 · 16 revisions

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.

Example Code

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();

Third-party implementations

Although the Http component has its own implementation of the PSR-07 standard, Slytherin supports the following third-party packages below that can be integrated seamlessly to Slytherin core once their respective packages were installed:

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 to use it by the Slytherin core.