Cors Illuminate

CORS (Cross-Origin Resource Sharing) support for Laravel and Lumen
Alternatives To Cors Illuminate
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Api9,2621,5751662 years ago72April 30, 2021198bsd-3-clausePHP
A RESTful API package for the Laravel and Lumen frameworks.
Lumen7,59277669 months ago22November 24, 2020PHP
The Laravel Lumen Framework.
Laravel Swoole3,983176a month ago55April 13, 202337mitPHP
High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
Laravel S3,7809810 hours ago227June 06, 202369mitPHP
LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.
Laravel Apidoc Generator3,385124263 months ago102May 28, 202055mitPHP
Laravel API Documentation Generator
Laravel Log Viewer3,048663642 months ago81February 15, 202341mitPHP
:dromedary_camel: Laravel log viewer
Seotools2,855166454 months ago47February 15, 202320mitPHP
SEO Tools for Laravel
Laravel Wechat2,809345577 months ago72October 17, 20221mitPHP
微信 SDK for Laravel, 基于 overtrue/wechat
Laravel Auditing2,74512259a month ago123June 19, 202311mitPHP
Record the change log from models in Laravel
Laravel Snappy2,450454532 months ago22February 05, 202334mitPHP
Laravel Snappy PDF
Alternatives To Cors Illuminate
Select To Compare


Alternative Project Comparisons
Readme

Project Management Scrutinizer Code Quality Code Coverage Build Status License

Description

This package adds Cross-Origin Resource Sharing (CORS) support to your Laravel application.

The package is based on Framework agnostic (PSR-7) CORS implementation.

The current version V3 is designed for Laravel 6 or higher. If you use lower Laravel version please use V2.

Install

1 Composer

composer require neomerx/cors-illuminate

2.1 Laravel

For Lumen skip this step and see step 2.2

Create a config file by executing

php artisan vendor:publish --provider="Neomerx\CorsIlluminate\Providers\LaravelServiceProvider"

it will create config/cors-illuminate.php file in you application.

Add CORS middleware to your HTTP stack at app/Http/Kernel.php file. The middleware should be added to $middleware list which is executed for all routes (even non declared in your routes file). Preferably before 'heavy' middleware for performance reasons.

class Kernel extends HttpKernel
{
    ...

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Neomerx\CorsIlluminate\CorsMiddleware::class, // <== add this line
        
        ...
    ];
    
    ...
}

Next see step 3

2.2 Lumen

For Laravel skip this step

In bootstrap/app.php add CORS to global middleware list

$app->middleware([
    ...
    \Neomerx\CorsIlluminate\CorsMiddleware::class,
]);

and register CORS provider

$app->register(\Neomerx\CorsIlluminate\Providers\LumenServiceProvider::class);

As Lumen does not support vendor:publish command file vendor/neomerx/cors-illuminate/config/cors-illuminate.php have to be manually copied to config/cors-illuminate.php.

Next see step 3

3 Configuration

Configuration file is extensively commented so it will be easy for you to set up it for your needs. First settings you need to configure are server origin (URL) and allowed origins

    ...
    
    /**
     * Could be string or array. If specified as array (recommended for
     * better performance) it should be in parse_url() result format.
     */
    Settings::KEY_SERVER_ORIGIN => [
        'scheme' => 'http',
        'host'   => 'localhost',
        'port'   => 1337,
    ],

    /**
     * A list of allowed request origins (no trail slashes).
     * If value is not on the list it is considered as not allowed.
     * If you want to allow all origins remove/comment this section.
     */
    Settings::KEY_ALLOWED_ORIGINS => [
        'http://localhost:4200',
    ],
    
    ...

Exceptions and CORS headers

When exceptions are thrown and responses are created in Laravel/Lumen exception handlers middleware will be excluded from handling responses. It means CORS middleware will not add its CORS headers to responses. For this reason CORS results (including headers) are registered in Laravel/Lumen Container and made accessible from any part of your application including exception handlers.

Code sample for reading CORS headers

use Neomerx\Cors\Contracts\AnalysisResultInterface;

$corsHeaders = [];
if (app()->resolved(AnalysisResultInterface::class) === true) {
    /** @var AnalysisResultInterface $result */
    $result = app(AnalysisResultInterface::class);
    $corsHeaders = $result->getResponseHeaders();
}

Customization

This package provides a number of ways how its behaviour could be customized.

The following methods of class CorsMiddleware could be replaced in descendant classes

  • getResponseOnError You can override this method in order to customize error reply.
  • getCorsAnalysis You can override this method to modify how CORS analysis result is saved to Illuminate Container.
  • getRequestAdapter You can override this method to replace IlluminateRequestToPsr7 adapter with another one.

Additionally a custom AnalysisStrategyInterface could be injected by

  • overriding getCreateAnalysisStrategyClosure method in ServiceProvider for Laravel/Lumen
  • using Laravel/Lumen Container binding for interface AnalysisStrategyInterface

Also custom AnalyzerInterface could be injected by

Testing

composer test

Contributing

Pull requests for documentation and code improvements (PSR-2, tests) are welcome.

Versioning

This package is using Semantic Versioning.

License

Apache License (Version 2.0). Please see License File for more information.

Popular Lumen Projects
Popular Laravel Projects
Popular Frameworks Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Php
Laravel
Cors
Lumen