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

Cross-domain middleware compatibility issues after using Dingo API #28

Open
barretzhi opened this issue Apr 17, 2018 · 21 comments
Open
Assignees
Labels
compatibility Issue about compatibility

Comments

@barretzhi
Copy link

barretzhi commented Apr 17, 2018

跨域中间件用的LumenCors , 每次php artisan laravels start 后 前几次 Access-Control-Allow-Origin:* 响应头还是成功的, 几次请求一走 响应头设置的cors 中间件就无效了
_ _20180417174001
_ _20180417174021

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 17, 2018

是否使用的PHP原生header()函数?
不能使用的函数

@barretzhi
Copy link
Author

@hhxsv5 我看了 那个包没有使用原生的header()
_ _20180418084656

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 18, 2018

第三方包吗?文件发下。

@barretzhi
Copy link
Author

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 18, 2018

测试了,没问题。
4348e083-4a3e-4d40-bd94-87143f20851e

@barretzhi
Copy link
Author

我也测试没问题,但请求几次就不行了
_ _20180418101752

@barretzhi
Copy link
Author

_ _20180418103047

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 18, 2018

body数据都正常吗?怀疑代码没走到中间件。

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 18, 2018

@barretzhi 细聊 QQ群:698480528

@barretzhi
Copy link
Author

body 正常的,我也在怀疑没走到中间件。 我发现服务启动后bootstarp/app.php 下的文件在每次请求的时候都不走

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 19, 2018

Dingo Api中干掉了Application的全局中间件。
https://github.com/dingo/api/blob/master/src/Routing/Adapter/Lumen.php#L253
目前还没有较好的办法解决,我再想想看

@barretzhi
Copy link
Author

好的,谢谢

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 25, 2018

不用中间件实现CORS,直接Response对象带上Header吧。这是目前较理想的解决方案。

public function index(Request $req)
{
    $headers = [
        'Access-Control-Allow-Origin' => '*',
        //...
    ];
    $data = ['now' => time()];
    return response()->json($data, 200, $headers);
}

然后,再统一封装一个Trait来做响应。

@barretzhi
Copy link
Author

哈哈,我是这样做的

@hhxsv5 hhxsv5 self-assigned this Apr 25, 2018
@hhxsv5 hhxsv5 added the compatibility Issue about compatibility label Apr 25, 2018
@hhxsv5 hhxsv5 changed the title 跨域HTTP中间件不能使用 Dingo Api导致跨域HTTP中间件不能使用 May 7, 2018
@dickwu
Copy link

dickwu commented Sep 2, 2018

跨域问题可以用nginx解决,这样又快又不需要引入那个包

@hhxsv5
Copy link
Owner

hhxsv5 commented Sep 2, 2018

@dickwu 也不错

@Eli-jah
Copy link

Eli-jah commented Dec 28, 2018

Problem solved partially, at least by now. Here is the solution:

Coding @ config/laravels.php as follows:

'register_providers' => [
        /* middleware service providers */
        Dingo\Api\Provider\DingoServiceProvider::class,
        Dingo\Api\Provider\HttpServiceProvider::class,
        /* auth service providers */
        Illuminate\Auth\AuthServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
],

Git it a try, and wish it would work for you, too.

@hhxsv5 hhxsv5 changed the title Dingo Api导致跨域HTTP中间件不能使用 Cross-domain middleware compatibility issues after using Dingo API Jul 26, 2019
@uniteweiwei
Copy link

image

image

@FlameMida
Copy link

目前还是不支持dingo吗?

@hhxsv5
Copy link
Owner

hhxsv5 commented Apr 13, 2022

@FlameMida 是的

@dengao123
Copy link

我这也是请求一段时间就报跨域错误,没用djingo。用的\Medz\Cors\Laravel\Middleware\ShouldGroup中间件.求大佬帮忙,卡了2,3天了。

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

No branches or pull requests

7 participants