• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 3 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CORS (Cross-Origin Resource Sharing) for your Symfony/Laravel requests

CORS for PHP (using the Symfony HttpFoundation)

Unit Tests PHPStan Level 9 Code Coverage Packagist License Latest Stable Version Total Downloads Fruitcake

Library and middleware enabling cross-origin resource sharing for your http-{foundation,kernel} using application. It attempts to implement the W3C Recommendation for cross-origin resource sharing.

Note: This is a standalone fork of https://github.com/asm89/stack-cors and is compatible with the options for CorsService.

Installation

Require fruitcake/php-cors using composer.

Usage

This package can be used as a library. You can use it in your framework using:

Options

Option Description Default value
allowedMethods Matches the request method. []
allowedOrigins Matches the request origin. []
allowedOriginsPatterns Matches the request origin with preg_match. []
allowedHeaders Sets the Access-Control-Allow-Headers response header. []
exposedHeaders Sets the Access-Control-Expose-Headers response header. []
maxAge Sets the Access-Control-Max-Age response header. 0
supportsCredentials Sets the Access-Control-Allow-Credentials header. false

The allowedMethods and allowedHeaders options are case-insensitive.

You don't need to provide both allowedOrigins and allowedOriginsPatterns. If one of the strings passed matches, it is considered a valid origin. A wildcard in allowedOrigins will be converted to a pattern.

If ['*'] is provided to allowedMethods, allowedOrigins or allowedHeaders all methods / origins / headers are allowed.

Note: Allowing a single static origin will improve cacheability.

Example: using the library

<?php

use Fruitcake\Cors\CorsService;

$cors = new CorsService([
    'allowedHeaders'         => ['x-allowed-header', 'x-other-allowed-header'],
    'allowedMethods'         => ['DELETE', 'GET', 'POST', 'PUT'],
    'allowedOrigins'         => ['http://localhost', 'https://*.example.com'],
    'allowedOriginsPatterns' => ['/localhost:\d/'],
    'exposedHeaders'         => ['Content-Encoding'],
    'maxAge'                 => 0,
    'supportsCredentials'    => false,
]);

$cors->addActualRequestHeaders(Response $response, $origin);
$cors->handlePreflightRequest(Request $request);
$cors->isActualRequestAllowed(Request $request);
$cors->isCorsRequest(Request $request);
$cors->isPreflightRequest(Request $request);

License

Released under the MIT License, see LICENSE.

This package is split-off from https://github.com/asm89/stack-cors and developed as stand-alone library since 2022

More Repositories

1

laravel-cors

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
PHP
6,258
star
2

laravel-telescope-toolbar

A toolbar for Laravel Telescope, based on the Symfony Web Profiler.
PHP
772
star
3

laravel-performance-monitor

Laravel Performance Monitor for Incoming Requests
PHP
33
star
4

magerun-modman

Magerun Modman command
PHP
27
star
5

magento2-custom-image-url

Magento2 Custom Image URLS for the catalog (like Imgproxy)
PHP
22
star
6

php-inbound-mail

PHP Inhound Mail parser for different clients (Sendgrid, Gmail, Postmark etc)
PHP
20
star
7

php-recaptcha

reCAPTCHA library
PHP
16
star
8

magento2-email-advanced-config

Advanced SMTP for Magento2
PHP
16
star
9

php-decimal

PHP
14
star
10

magento2-image-optimizer

Image Optimizer for Magento2
PHP
9
star
11

magento2-b2b-essentials

Magento2 B2B Essentials
PHP
7
star
12

laravel-recaptcha

Laravel integration for reCAPTCHA Library
PHP
7
star
13

omnipay-sisow

Sisow gateway for Omnipay
PHP
6
star
14

laravel-apn-notification-channel

Laravel APN (Apple Push) channel for Notifications
PHP
6
star
15

magento2-alwaysloginascustomer

Magento2 - Always allow Login as Customer
PHP
5
star
16

php-google-cloudprint

PHP
5
star
17

laravel-gcm-notification-channel

Google Android GCM Notification Channel for Laravel
PHP
4
star
18

php-aws-iot-manager

PHP
4
star
19

omnipay-pesapal

Omnipay Pesapal gateway
PHP
2
star
20

php-knvb-dataservice-api

KNVB Dataservice API Wrapper
PHP
2
star
21

magento2-essentials

Magento2 Essentials
1
star
22

magento2-dev-essentials

Magento2 dev essentials
PHP
1
star
23

omnipay-magnius

Omnipay gateway for Magnius
PHP
1
star
24

php-honeywell-client

Honeywell client for honeywell T-type Thermostats
PHP
1
star