-
Notifications
You must be signed in to change notification settings - Fork 4
Http
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.
To integrate a HTTP component to Slytherin, you need to implement them in both ServerRequestInterface
and ResponseInterface
interfaces as defined in PSR-7 standard. 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 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.
$container = new Rougin\Slytherin\Container\Container;
// Define HTTP objects that is compliant to PSR-7 standards
$request = new Rougin\Slytherin\Http\ServerRequest($_SERVER);
$response = new Rougin\Slytherin\Http\Response(http_response_code());
$container->set('Psr\Http\Message\ServerRequestInterface', $request);
$container->set('Psr\Http\Message\ResponseInterface', $response);