Cross-origin resource sharing (CORS) Middleware for PHP Slim Framework.
Install with Composer
- Update your
composer.json
to requirepalanik/corsslim
package. - Run
composer install
to add CorsSlim your vendor folder.
{
"require": {
"palanik/corsslim": "*"
}
}
<?php
require ('./vendor/autoload.php');
$app = new \Slim\Slim();
$app->add(new \CorsSlim\CorsSlim());
?>
<?php
\Slim\Slim::registerAutoLoader();
$app = new \Slim\Slim();
require ('path_to_your_middlewares/CorsSlim.php');
$app->add(new \CorsSlim\CorsSlim());
?>
You can create the middleware with custom options. Pass options as associative array.
origin
=> The value to set for Access-Control-Allow-Origin response header. Default value is '*'.exposeHeaders
=> The value to set for Access-Control-Expose-Headers response header. Pass an array of strings.maxAge
=> The value to set for Access-Control-Max-Age response header.allowCredentials
=> The value to set for Access-Control-Allow-Credentials response header. Pass True/False.allowMethods
=> The value to set for Access-Control-Allow-Methods response header. Pass an array of allowed method names.allowHeaders
=> The value to set for Access-Control-Allow-Headers response header. Pass an array of allowed headers.
$corsOptions = array(
"origin" => "*",
"exposeHeaders" => array("X-My-Custom-Header", "X-Another-Custom-Header"),
"maxAge" => 1728000,
"allowCredentials" => True,
"allowMethods" => array("POST, GET"),
"allowHeaders" => array("X-PINGOTHER")
);
$cors = new \CorsSlim\CorsSlim($corsOptions);