PHP code example of f3-factory / fatfree-psr7-factory

1. Go to this page and download the library: Download f3-factory/fatfree-psr7-factory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

f3-factory / fatfree-psr7-factory example snippets


// create the PSR17 adapter
$psrAdapter = F3\Http\MessageFactory::instance();
// register the factories:
$psrAdapter->register(
    requestFactory:       Psr17Factory::class,
    responseFactory:      Psr17Factory::class,
    serverRequestFactory: Psr17Factory::class,
    uploadedFileFactory:  Psr17Factory::class,
    uriFactory:           Psr17Factory::class,
    streamFactory:        Psr17Factory::class,
);
// register the concrete Request / Response objects 
$psrAdapter->registerRequest(RequestInterface::class);
$psrAdapter->registerResponse(ResponseInterface::class);
$psrAdapter->registerServerRequest(ServerRequestInterface::class);

MessageFactory::registerDefaults()

$f3->route('GET /hallo-world', function(
    Base $f3, 
    RequestInterface $request, 
    ResponseInterface $response, 
    StreamFactoryInterface $streamFactory
) {
    $agent = $request->getHeaderLine('User-Agent');
    return $response->withBody($streamFactory
        ->createStream('Your user agent is: '.$agent));
});

$request = $f3->make(RequestInterface::class);
// or 
$request = MessageFactory::instance()->makeRequest();

// for Server Request:
$serverRequest = $f3->make(ServerRequestInterface::class);
// or 
$serverRequest = MessageFactory::instance()->makeServerRequest();

$f3->route('POST /upload', function(Base $app, ServerRequestInterface $request)
{
    $dir = './uploads/';
    // handle uploaded files
    $files = $request->getUploadedFiles();
    foreach ($files as $fieldName => $file) {
        if ($file instanceof UploadedFile) {
            $file->moveTo($dir.$file->getClientFilename());
        }
    }
};