PHP code example of makise-co / stack-cors

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

    

makise-co / stack-cors example snippets


return [

    /*
     * You can enable CORS for 1 or multiple paths.
     * Example: ['api/*']
     */
    'paths' => ['*'],

    /*
    * Matches the request method. `[*]` allows all methods.
    */
    'allowedMethods' => ['*'],

    /*
     * Matches the request origin. `[*]` allows all origins.
     */
    'allowedOrigins' => ['*'],

    /*
     * Matches the request origin with, similar to `Request::is()`
     */
    'allowedOriginsPatterns' => [],

    /*
     * Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
     */
    'allowedHeaders' => ['*'],

    /*
     * Sets the Access-Control-Expose-Headers response header.
     */
    'exposedHeaders' => false,

    /*
     * Sets the Access-Control-Max-Age response header.
     */
    'maxAge' => 600,

    /*
     * Sets the Access-Control-Allow-Credentials header.
     */
    'supportsCredentials' => true,

];



use Asm89\Stack\CorsService;

$cors = new CorsService(array(
    'allowedHeaders'         => array('x-allowed-header', 'x-other-allowed-header'),
    'allowedMethods'         => array('DELETE', 'GET', 'POST', 'PUT'),
    'allowedOrigins'         => array('http://localhost'),
    'allowedOriginsPatterns' => array('/localhost:\d/'),
    'exposedHeaders'         => false,
    'maxAge'                 => false,
    'supportsCredentials'    => false,
));

$cors->addActualRequestHeaders(Response $response, $origin);
$cors->handlePreflightRequest(Request $request);
$cors->isActualRequestAllowed(Request $request);
$cors->isCorsRequest(Request $request);
$cors->isPreflightRequest(Request $request);