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

This package is not auto-updated.

Last update: 2024-04-13 13:14:58 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.

Master Build Status

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
    }
});