Skip to content
Rougin Gutib edited this page Apr 7, 2017 · 16 revisions

In PHP, there are built in functions that can work in HTTP. There is also a list of functions that are related to it and it can be found here. However, if you need to implement it in a nice and clean way, you will need a package with a nice object-oriented interface that can be integrated easily to your application.

Example

To integrate a HTTP component to Slytherin, you need to implement them in both ServerRequestInterface and ResponseInterface interfaces as included in PSR HTTP Message. You can also read the meta document about the implementation here.

Why do we need to implement two classes for a HTTP component if we can have it in a one cool class? They are needed to be seperated 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.

As of 0.4.0, Slytherin's sample implementation of Http component is removed. The reason is for Slytherin to adopt the PSR-7 implementations. With that, you will just need to find a package that is PSR-7 compliant and set it via ComponentCollection after.

$components = new Rougin\Slytherin\Component\Collection;

// ...

$request  = new Acme\Http\ServerRequest;
$response = new Acme\Http\Response;

$components->setHttp($request, $response);