PHP code example of componenta / http-cors-middleware

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

    

componenta / http-cors-middleware example snippets


use Componenta\Http\Middleware\Cors\CorsConfiguration;
use Componenta\Http\Middleware\Cors\CorsMiddleware;

$config = new CorsConfiguration(
    allowedOrigins: ['https://app.example.com'],
    allowedMethods: ['GET', 'POST', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'Authorization', 'X-CSRF-Token'],
    exposedHeaders: ['X-Request-Id'],
    allowCredentials: true,
    maxAge: 600,
);

$middleware = new CorsMiddleware($config, $responseFactory);

new CorsConfiguration(
    allowedOrigins: ['*'],
    allowedMethods: ['GET'],
);

new CorsConfiguration(
    allowedOrigins: ['https://app.example.com'],
    allowedMethods: ['POST'],
    allowedHeaders: ['Content-Type', 'Authorization'],
    exposedHeaders: ['X-Request-Id'],
    allowCredentials: true,
);

new CorsConfiguration(
    allowedOrigins: ['https://admin.example.com'],
    allowedMethods: ['POST'],
    allowPrivateNetwork: true,
);