Quickstart
This example shows the most common beginner workflow:
- register the provider
- resolve the convenience factories
- create common responses without manually building bodies and headers
use FastForward\Config\ArrayConfig;
use FastForward\Container\ContainerInterface;
use FastForward\Container\container;
use FastForward\Http\Message\Factory\ResponseFactoryInterface;
use FastForward\Http\Message\Factory\ServiceProvider\HttpMessageFactoryServiceProvider;
use FastForward\Http\Message\Factory\StreamFactoryInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
$config = new ArrayConfig([
ContainerInterface::class => [
HttpMessageFactoryServiceProvider::class,
],
]);
$container = container($config);
$requestFactory = $container->get(RequestFactoryInterface::class);
$serverRequest = $container->get(ServerRequestInterface::class);
$responseFactory = $container->get(ResponseFactoryInterface::class);
$streamFactory = $container->get(StreamFactoryInterface::class);
$request = $requestFactory->createRequest('GET', '/health');
$jsonResponse = $responseFactory->createResponseFromPayload([
'ok' => true,
'path' => (string) $serverRequest->getUri(),
]);
$htmlResponse = $responseFactory->createResponseFromHtml('<h1>Welcome</h1>');
$redirectResponse = $responseFactory->createResponseRedirect('/login');
$noContentResponse = $responseFactory->createResponseNoContent([
'X-Request-Handled' => 'true',
]);
$payloadStream = $streamFactory->createStreamFromPayload([
'queued' => true,
]);
$acceptedResponse = $responseFactory
->createResponse(202)
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withBody($payloadStream);
What To Notice
RequestFactoryInterfaceis the standard PSR-17 request factoryServerRequestInterfaceis created from PHP globals when you resolve it from the containerFastForward\Http\Message\Factory\ResponseFactoryInterfaceadds helper methods on top of PSR-17FastForward\Http\Message\Factory\StreamFactoryInterfaceaddscreateStreamFromPayload()
Next Steps
- Read Getting Services to understand which interface to resolve in each situation.
- Read JSON Response and Stream Usage if you are building APIs.