Cross-origin resource sharing library for the Symfony HttpFoundation

fruitcake/php-cors image

fruitcake/php-cors stats

Downloads
48.5M
Stars
161
Open Issues
3
Forks
10

View on GitHub →

CORS for PHP by Fruitcake

The fruitcake/php-cors package provides a robust solution for enabling Cross-Origin Resource Sharing (CORS) in applications using PHP's Symfony HttpFoundation. This library is essential for developers needing to manage CORS policies efficiently to allow or restrict resources shared between different origins.

Key Features

  • Flexible CORS Rules: Define which HTTP methods, headers, and origins are allowed.
  • Pattern Matching: Utilize regular expressions to define allowed origins.
  • Support for Credentials: Control whether to expose credentials.
  • Configurable Response Headers: Set headers for exposed headers and maximum cache age.
  • High Compatibility: Works seamlessly as middleware for StackPHP and can be integrated into Laravel through an additional package.

Installation

Install the package via Composer:

composer require fruitcake/php-cors

Usage

Basic Configuration

You can easily integrate CORS handling within your application by configuring the following options:

  • allowedMethods: Specify which HTTP methods are permitted.
  • allowedOrigins: Define which origins can access the resources.
  • allowedHeaders: Set specific headers that can be used during the request.
  • exposedHeaders: Decide which headers are exposed in the response.
  • maxAge: Determine the time for which the response is considered valid.
  • supportsCredentials: Enable or disable cookies and authentication data.

Practical Example

Here’s how you can set up the CORS service in your PHP application:

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'],
'exposedHeaders' => ['Content-Encoding'],
'maxAge' => 0,
'supportsCredentials' => false,
]);
 
$response = new Response();
$cors->addActualRequestHeaders($response, $origin);

Advanced Handling

The package also provides methods to handle preflight requests and check if a request is allowed under the defined CORS policy.

License

This package is released under the MIT License.

For developers looking to implement CORS in their PHP applications effectively, fruitcake/php-cors offers a powerful, flexible solution that is easy to integrate and configure. Whether you are developing a simple site or a complex RESTful service, this package is designed to meet your CORS requirements.

fruitcake photo

Your #1 dev team

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Fruitcake Php Cors Related Articles

Diving into Cross-Origin Resource Sharing image

Diving into Cross-Origin Resource Sharing

Read article
Laravel 9.2 Released image

Laravel 9.2 Released

Read article
Curotec logo

Curotec

Hire the top Latin American Laravel talent. Flexible engagements, budget optimized, and quality engineering.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce