Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cors #4

Open
Maclay74 opened this issue Feb 20, 2021 · 4 comments
Open

Cors #4

Maclay74 opened this issue Feb 20, 2021 · 4 comments

Comments

@Maclay74
Copy link

It would be great to have them out of the box, because without headers it doesn't work at all.

@hoaaah
Copy link
Owner

hoaaah commented Feb 23, 2021

I don't get it? Please elaborate the use case.

@Maclay74
Copy link
Author

Sure!

When you use API for a single-page application, like created on react or something, you usually locate them on different domains. Moreover, during development they can be both on localhost but on different ports.

And by default, cors wouldn't allow to make requests from frontend to backend, if backend doesn't provide client with special headers.

It's only a browser's thing, mobile apps don't rely on cors.

So, my workaround was to add them like that

 $behaviors['corsFilter'] = [
            'class' => '\yii\filters\Cors',
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['*'],
            ],
        ];

But I created a BaseController class and extend it everytime instaed of ActiveController, because my application is built this way.

@hoaaah
Copy link
Owner

hoaaah commented Mar 3, 2021

Oh right. I'm using this template for mobile device and web services. I still can't get the best approach for this feature.

Will all controller use this? If we add corsFilter to BehaviorsFromParamsHelper then this template will be difficult to extend. It might be enough to add this case in to documentation, including the option to use a modified BaseController as your example.

Do you have a better idea?

@Maclay74
Copy link
Author

Maclay74 commented Mar 3, 2021

I use BaseController with behaviours, but you can just mention this part in README, so user will handle it somehow.
Just an advice with code sample would be great, we deal with developers, anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants