Code Monkey home page Code Monkey logo

cors-provider's Introduction

FastD Cors Provider

Installation

composer require fastd/cors-provider

Usage

复制 vendor/fastd/cors-provider/config/cors.phpconfig/cors.php, 并修改配置

<?php
return [
    'allow_credentials' => true,
    'origins' => ['*'],
    'methods' => ['GET', 'POST', 'HEAD', 'DELETE', 'PATCH', 'PUT'],
    'headers' => ['*'],
    'exposed_headers' => ['x-custom'],
    'max_age' => null,
];

配置 config/routes.php

<?php
use FastD\CORSProvider\CorsMiddleware;
use \FastD\Routing\RouteCollection;

route()->middleware(new CorsMiddleware(), function (RouteCollection $router) {
    $router->get('/demo', function () {
        return json([
            'foo' => 'bar',
        ]);
    });
});

完成 !

执行 curl -i 'http://127.0.0.1:8889/demo' -XGET

响应:

HTTP/1.1 200
Host: 127.0.0.1:8889
Date: Sat, 28 Apr 2018 17:08:47 +0800
Connection: close
X-Powered-By: PHP/7.2.4
Content-Type: application/json; charset=UTF-8

{"foo":"bar"}

执行 curl -i 'http://127.0.0.1:8889/demo' -XGET -H 'Origin: http://127.0.0.1:8889'

响应:

HTTP/1.1 200
Host: 127.0.0.1:8889
Date: Sat, 28 Apr 2018 17:09:35 +0800
Connection: close
X-Powered-By: PHP/7.2.4
Content-Type: application/json; charset=UTF-8
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://127.0.0.1:8889
Vary: Origin
Access-Control-Expose-Headers: x-custom

{"foo":"bar"}

执行 curl -i 'http://127.0.0.1:8889/demo' -XOPTIONS -H 'access-control-request-method: GET'

响应:

HTTP/1.1 200
Host: 127.0.0.1:8889
Date: Sat, 28 Apr 2018 17:10:31 +0800
Connection: close
X-Powered-By: PHP/7.2.4
Access-Control-Allow-Methods: GET,POST,HEAD,DELETE,PATCH,PUT
Access-Control-Max-Age: 200
Content-type: text/html; charset=UTF-8

执行 curl -i 'http://127.0.0.1:8889/demo' -XOPTIONS -H 'access-control-request-method: GET' -H 'access-control-request-headers: hello'

响应:

HTTP/1.1 200
Host: 127.0.0.1:8889
Date: Sat, 28 Apr 2018 17:11:27 +0800
Connection: close
X-Powered-By: PHP/7.2.4
Access-Control-Allow-Methods: GET,POST,HEAD,DELETE,PATCH,PUT
Access-Control-Allow-Headers: hello
Access-Control-Max-Age: 200
Content-type: text/html; charset=UTF-8

cors-provider's People

Contributors

janhuang avatar runnerlee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.