demo icon indicating copy to clipboard operation
demo copied to clipboard

How to setup CORS in yii3 ?

Open ayiangio opened this issue 5 years ago • 5 comments

What steps will reproduce the problem?

i have done to setup on web server in this case i use apache2

What is the expected result?

my API can run on another domain

Additional info

Q A
Version 1.0.?
PHP version 7.4.7
Operating system Ubuntu 18.04

ayiangio avatar Jul 01 '20 07:07 ayiangio

We don't yet have our own CORS middleware implementation, but you can use any psr-7/psr-15 compatible middleware. For example https://github.com/tuupola/cors-middleware

Route::get('/info/v2', ApiInfo::class)
    ->addMiddleware(FormatDataResponseAsJson::class)
    ->addMiddleware(fn() => new Tuupola\Middleware\CorsMiddleware([
        "origin" => ["*"],
        "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"],
        "headers.allow" => [],
        "headers.expose" => [],
        "credentials" => false,
        "cache" => 0,
   ]))
   ->name('api/info/v2'),

yiiliveext avatar Jul 01 '20 08:07 yiiliveext

image image

@samdark @yiiliveext the code you have given doesn't work in my case

ayiangio avatar Jul 06 '20 09:07 ayiangio

@ayiangio what are headers that are sent?

samdark avatar Jul 06 '20 13:07 samdark

We want to send two headers

  1. Authorization for token
  2. Role user

@samdark

ayiangio avatar Jul 08 '20 02:07 ayiangio

OK but what headers are actually sent after you configure CSRF middleware and make a request?

samdark avatar Jul 08 '20 09:07 samdark