andreiashu / silex-stackcors-provider
基于 asm89/stack-cors 库的跨域资源共享库的 Silex Provider
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-09-28 15:43:23 UTC
README
Silex 服务提供程序,允许您的 Silex 应用程序进行跨域资源共享。它基于 Stack/Cors 库,用于处理请求/响应。
安装
使用 composer 安装 andreiashu/silex-stackcors-provider
。
用法
更多选项请参阅 Stack/Cors 库的 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 } });