PHP code example of decodelabs / harvest

1. Go to this page and download the library: Download decodelabs/harvest 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/ */

    

decodelabs / harvest example snippets


use DecodeLabs\Harvest;
use DecodeLabs\Harvest\Dispatcher;
use DecodeLabs\Harvest\Middleware\ContentSecurityPolicy;
use DecodeLabs\Harvest\Profile;
use DecodeLabs\Harvest\Response\Text as TextResponse;
use DecodeLabs\Monarch;

// Create a Middleware Profile
$profile = new Profile(
    'ErrorHandler', // Resolve by name via container / Archetype

    new ContentSecurityPolicy(), // Add middleware instance

    function($request, $handler) {
        // Add middleware callback
        // $handler is the next middleware in the stack
        // $request is the current request

        // Return a response
        return new TextResponse('Hello World!');
    }
);

// Create a Dispatcher
$dispatcher = new Dispatcher($profile);
$harvest = Monarch::getService(Harvest::class);

$request = $harvest->createRequestFromEnvironment();
$response = $dispatcher->dispatch($request);

use DecodeLabs\Harvest\Profile;

$profile = new Profile()
    ->add('Cors', priority: 5, group: 'Generic')
    ->add(new SomeOtherVendorMiddleware(), priority: 10, group: 'Inbound');

use DecodeLabs\Harvest\ResponseHandler;
use DecodeLabs\Harvest\Transport\Native as NativeTransport;

$handler = new ResponseHandler(
    new NativeTransport()
);

$handler->sendResponse(
    $request,
    $response
);

exit;

use DecodeLabs\Harvest\Response\Text as TextResponse;
use DecodeLabs\Harvest\Response\Html as HtmlResponse;
use DecodeLabs\Harvest\Response\Json as JsonResponse;
use DecodeLabs\Harvest\Response\Xml as XmlResponse;
use DecodeLabs\Harvest\Response\Redirect as RedirectResponse;
use DecodeLabs\Harvest\Response\Stream as StreamResponse;
use DecodeLabs\Harvest\Response\Generator as GeneratorResponse;

$text = new TextResponse('Hello World!'); // Text

$customText = new TextResponse('Hello World!', 201, [
    'Custom-Header' => 'header-value'
]);

$html = new HtmlResponse('<h1>Hello World!</h1>'); // HTML

$json = new JsonResponse([
    'whatever-data' => 'Hello World!'
]); // JSON

$xml = new XmlResponse($xmlString); // XML

$redirect = new RedirectResponse('/some/other/path'); // Redirect

$file = new StreamResponse('/path/to/file'); // Stream

$resource = new StreamResponse(
    $harvest->createStreamFromResource($resource)
); // Stream

$generator = new GeneratorResponse(function() {
    yield 'Custom content';
    yield ' can be written';
    yield ' and streamed';
    yield ' from a generator';
}, 200, [
    'Content-Type' => 'text/plain'
]);

$profile->add('Cookies');

$harvest->cookies->set(
    name: 'cookie-name',
    value: 'cookie-value',
    domain: 'example.com',
    path: '/',
    expires: '10 minutes',
    maxAge: 600,
    httpOnly: true,
    secure: true,
    sameSite: 'Strict',
    partitioned: true
);

$harvest->cookies->expire(
    name: 'cookie-name',
    domain: 'example.com',
    path: '/',
);