psr/http-factory
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The psr/http-factory is a composer package that provides interface-related elements for PSR-17 HTTP factories. PSR-17 encompasses HTTP factories that are utilized in the creation of HTTP objects while adhering to certain standards. It's important to note that the package doesn't have a HTTP Factory implementation of its own, but rather, it details the components that define a HTTP Factory.
To use psr/http-factory, you must first require it in your PHP project using Composer. This can be done by running the command composer require psr/http-factory
. Using psr/http-factory largely depends on the specific PSR-17 factory implementation you need. Here is a basic example of usage:
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
class MyClass
{
private $responseFactory;
private $streamFactory;
public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
{
$this->responseFactory = $responseFactory;
$this->streamFactory = $streamFactory;
}
public function doSomething(): ResponseInterface
{
$response = $this->responseFactory->createResponse(200);
$stream = $this->streamFactory->createStream('hello world');
$response = $response->withBody($stream);
return $response;
}
}
In the example given, we're injecting our PSR-17 factories into our class and then using them to generate a response. Although our dependency injection needs aren't addressed in this example, they would be handled in our container or factory that creates MyClass
.
The official documentation for the psr/http-factory is hosted at the PHP-FIG website under PSR-17, which can be accessed directly from this link. In addition, relevant information about the package and its implementations can also be found on its Packagist listing and through the providers of psr/http-factory-implementations on Packagist.