yii-starter-kit / yii2-starter-kit

Yii2 Starter Kit
http://yii2-starter-kit.terentev.net
Other
1.42k stars 648 forks source link

multiple domain for api url? #729

Closed carlos-andres closed 4 years ago

carlos-andres commented 4 years ago

Good day

Is possible use same api url setting a.k.a API_HOST_INFO with two domains on .env or /config/web.php file.. I see multiple issues this week with CORS request also I try to change request formula using latest tutorials and in some cases I got this error. regards I put specific cors on the headers into api controllers

Access to XMLHttpRequest at 'https://api.{site}.com/v1/xxxx/' from origin 'https://{other-site}.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Thanks in advance

XzAeRo commented 4 years ago

Yes it is possible, but it's by far out of the scope of this project (this is just a starter template).

However, I had to deal with this issue before, and to handle it, you will have to either modify your .env file with another variable for the additional URL and read it from the config files, or just modifying directly into the web.php config file (remember, this is specifically for your project, so you're not doing any harm).

Also, you might need to update your web server settings. In Nginx you need to add the add_header Access-Control-Allow-Origin http://your.url/here directive for each domain that you want to "allow" to use this API. In the Nginx config files we have, you can put them where the php-fpm block is.

This article: https://developpaper.com/implementation-of-yiis-principle-of-supporting-multi-domain-cors/ helped me to understand a little better how to deal with it. However, dealing with CORS is almost like handling black magic at times.