A silex service provider that adds CORS services to silex
MIT License
The CorsServiceProvider provides CORS support as middleware for your silex application. CORS allows you to make AJAX requests across domains. CORS uses OPTIONS requests to make preflight requests. Because silex doesn't have functionality for serving OPTIONS request by default, this service goes through all of your routes and generates the necessary OPTIONS routes.
Install the silex-cors-provider using composer. This project uses sematic versioning.
composer require jdesrosiers/silex-cors-provider "~1.0"
Cache-Control
, Content-Language
, Content-Type
, Expires
, Last-Modified
, Pragma
.cors-enabled
instead)cors.*
parameters without the cors.
part. This is useful of you needcors-enabled
but only creates OPTIONS routes with no CORS support.Allow
header to every route the object defines.$app->register(new JDesrosiers\Silex\Provider\CorsServiceProvider(), [
"cors.allowOrigin" => "http://petstore.swagger.wordnik.com",
]);
Add CORS functionality to the entire application.
$app->get("/foo/{id}", function ($id) { /* ... */ });
$app->post("/foo/", function () { /* ... */ });
$app["cors-enabled"]($app);
Add CORS functionality to a controller collection.
$foo = $app["controllers_factory"];
$foo->get("/{id}", function () { /* ... */ });
$foo->post("/", function () { /* ... */ });
$app->mount("/foo", $app["cors-enabled"]($foo));
$app->get("/bar/{id}", function ($id) { /* ... */ }); // Not CORS enabled
Add CORS functionality to a controller.
$controller = $app->get("/foo/{id}", function ($id) { /* ... */ });
$app["cors-enabled"]($controller);
$app->post("/foo/", function () { /* ... */ }); // Not CORS enabled