PHP code example of effectra / cors

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

    

effectra / cors example snippets


use Effectra\Cors\CorsService;
use Effectra\Cors\CorsMiddleware;

$corsService = new CorsService([
    'allowedOrigins'     => ['https://example.com'],
    'allowedMethods'     => ['GET', 'POST', 'PUT', 'DELETE'],
    'allowedHeaders'     => ['Content-Type', 'Authorization'],
    'exposedHeaders'     => ['X-Custom-Header'],
    'supportsCredentials'=> false,
    'maxAge'             => 3600,
]);

$middleware = new CorsMiddleware($corsService, [
    'paths'          => ['api/*'],
    'excluded_paths' => ['api/internal/*'],
]);

new CorsService(array $options = [])

$corsService->setOptions([
    'allowedOrigins' => ['https://app.example.com'],
    'allowedMethods' => ['GET', 'POST'],
]);

$corsService->setDebugMode(true);

if ($corsService->isCorsRequest($request)) {
    // handle CORS
}

if ($corsService->isPreflightRequest($request)) {
    $response = $corsService->handlePreflightRequest($request);
}

$allowed = $corsService->isOriginAllowed($request); // true | false

$response = $corsService->handlePreflightRequest($request);

$response = $corsService->addPreflightRequestHeaders($response, $request);

$response = $corsService->addActualRequestHeaders($response, $request);

$response = $corsService->varyHeader($response, 'Origin');

$errors = $corsService->getErrors();
// ['origin' => "Origin 'http://evil.com' not allowed"]

if ($corsService->hasErrors()) {
    // inspect or log $corsService->getErrors()
}

$corsService->clearErrors();

new CorsMiddleware(CorsService $cors, array $config = [])

$middleware->setPaths(['api/*', 'webhooks/*']);

$middleware->setExcludedPaths(['api/internal/*']);

$middleware->setHandlePreflight(false); // delegate to the next handler

$middleware->setHandleErrors(false);

$service = $middleware->getCorsService();
$errors  = $service->getErrors();

'paths' => [
    'app.example.com' => ['dashboard/*'],
    'api.example.com' => ['v1/*', 'v2/*'],
],

$corsService->setDebugMode(true); // also writes to PHP error_log()

// After processing a request:
if ($corsService->hasErrors()) {
    print_r($corsService->getErrors());
}

// Clear for the next request
$corsService->clearErrors();

$corsService = new CorsService([
    'allowedOrigins' => ['*'],
    'allowedMethods' => ['*'],
    'allowedHeaders' => ['*'],
]);

$corsService = new CorsService([
    'allowedOrigins'      => ['https://*.example.com'],
    'allowedMethods'      => ['GET', 'POST'],
    'allowedHeaders'      => ['Content-Type', 'Authorization'],
    'supportsCredentials' => true,
]);

$middleware = new CorsMiddleware($corsService, [
    'paths'            => ['api/*'],
    'excluded_paths'   => ['api/health', 'api/internal/*'],
    'handle_preflight' => true,
    'handle_errors'    => true,
]);

$middleware = (new CorsMiddleware($corsService))
    ->setPaths(['api/*'])
    ->setExcludedPaths(['api/internal/*'])
    ->setHandlePreflight(true)
    ->setHandleErrors(true);

use Effectra\Cors\CorsService;

$corsService = new CorsService([
    'allowedOrigins' => ['https://frontend.example.com'],
    'allowedMethods' => ['GET', 'POST'],
    'allowedHeaders' => ['Content-Type'],
]);

if ($corsService->isPreflightRequest($request)) {
    return $corsService->handlePreflightRequest($request);
}

if ($corsService->isCorsRequest($request)) {
    $response = $corsService->addActualRequestHeaders($response, $request);
}