andreiashu / silex-stackcors-provider
Silex Provider for Cross-origin resource sharing library based on asm89/stack-cors library
dev-master
2014-02-10 16:46 UTC
Requires
- php: >=5.3.2
- asm89/stack-cors: *
- silex/silex: ~1.1
This package is not auto-updated.
Last update: 2024-10-26 15:57:05 UTC
README
Silex service provider enabling cross-origin resource sharing for your Silex application. It's based on the Stack/Cors Library in order to do request/response handling.
Installation
Require andreiashu/silex-stackcors-provider
using composer.
Usage
For more options see the Stack/Cors Library readme
<?php $app = new Silex\Application(); $cors_options = array( // allow all headers 'allowedHeaders' => array('*'), // allow requests from localhost only. Use '*' to allow any origins 'allowedOrigins' => array('localhost'), // optional: use a specific response class when the request is not allowed // should be a subclass of \Symfony\Component\HttpFoundation\Response // example 'denied_reponse_class' => '\Andreiashu\Silex\Provider\CorsServiceDeniedResponse' ); $app->register(new Andreiashu\Silex\Provider\CorsServiceProvider($cors_options)); // in a REST API you can add an ->after() hook to check if there are any CORS // errors and render your response according to your API error standards $app->after(function (Request $request, Response $response) use ($app) { if (is_a($response, '\Andreiashu\Silex\Provider\CorsServiceDeniedResponse')) { // alter the response object as needed } });