Thursday, January 11, 2024

Cors In LARAVEL

 Cors In LARAVEL


1 . Install Package 

            composer require fruitcake/laravel-cors


2 . Publish Configuration

        This command will create a config/cors.php file in your Laravel project.

            php artisan vendor:publish --provider="Fruitcake\Cors\CorsServiceProvider"

3 . Configure CORS Settings

        Open the config/cors.php file

              return [                     'paths' => ['api/*'],                     'allowed_methods' => ['*'],                     'allowed_origins' => ['*'],                     'allowed_origins_patterns' => [],                      'allowed_headers' => ['*'],                     'exposed_headers' => [],                      'max_age' => 0,                      'supports_credentials' => false, ];


4 . Update Middleware

        In your app/Http/Kernel.php file,

        add the Fruitcake\Cors\HandleCors middleware to the $middleware array:

           protected $middleware = [             // ...                 \Fruitcake\Cors\HandleCors::class,                ];


NOTE

If allow only the specified origin :

Ex:
(http://127.0.0.1:5500/) for POST requests using the fruitcake/laravel-cors package, you can configure the config/cors.php file accordingly.


return [
    'paths' => ['api/*'],

    'allowed_methods' => ['POST'],

    'allowed_origins' => ['http://127.0.0.1:5500'],
                                or many server    
    'allowed_origins' => ['http://127.0.0.1:5500' , 'http://localhost:3000'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,
];


           

No comments:

Post a Comment