Skip to content
Rougin Gutib edited this page Feb 28, 2018 · 16 revisions

In PHP, there are built-in functions that can work in HTTP. The list of functions that are related to it can be found here. However, a HTTP package with a nice object-oriented interface is greatly recommended.

Example

To integrate a HTTP component to Slytherin, a package must implement both ServerRequestInterface and ResponseInterface interfaces as defined in PSR-7 standard. A meta document about the implementation can be found here.

Why need to implement two classes for a HTTP component if it can be in a one cool class? They are needed to be separated because of the Single Responsibility Principle.

In object-oriented programming, the said principle states that every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. With that principle, it's easier for you to debug a certain problem in a class because you can easily determine the responsibility that is included to it.

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Container\Container;
use Rougin\Slytherin\Http\Response;
use Rougin\Slytherin\Http\ServerRequest;

$container = new Container;

$request = new ServerRequest($_SERVER);

$container->set(ServerRequestInterface::class, $request);

$response = new Response(http_response_code());

$container->set(ResponseInterface::class, $response);