PHP code example of davidecesarano / embryo-cors

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

    

davidecesarano / embryo-cors example snippets


use Embryo\CORS\CorsMiddleware;
use Embryo\Http\Factory\{ResponseFactory, ServerRequestFactory};
use Embryo\Http\Server\RequestHandler;

// Set options
$allowed_origins = ['*'];
$allowed_methods = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'];
$allowed_headers = ['Content-Type', 'Accept', 'Origin', 'Authorization'];
$exposed_headers = [];
$max_age = 0;
$supports_credentials = false;

// Add middleware to dispatcher
$requestHandler = new RequestHandler([
    (new CorsMiddleware)
        ->setAllowedOrigins($allowed_origins)
        ->setAllowedMethods($allowed_methods)
        ->setAllowedHeaders($allowed_headers)
        ->setExposedHeaders($exposed_headers)
        ->setMaxAge($max_age)
        ->setSupportsCredentials($supports_credentials)
]);

// Set PSR Request and Response
$request = (new ServerRequestFactory)->createServerRequestFromServer();
$response = (new ResponseFactory)->createResponse(200);

$response = $requestHandler->dispatch($request, $response);