dingo / dingo/api

Internal requests not nestable

Open
#1,164 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
9.4k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

I have some routes that call themselves, based on:

https://github.com/dingo/api/wiki/Internal-Requests

I have a notion of users, me and global scope where the various levels infer the current user. But I don't want to have copies of similar code in my controllers. Here is some pseudo code of what I'm trying to accomplish:

```
$api->addRoute(['POST'], 'users/{users}/objects', ['as' => 'api.users.objects.store', 'uses' => function ($userId) {
// process request here
}]);

$api->addRoute(['POST'], 'me/objects', ['as' => 'api.me.objects.store', 'uses' => function () {
return app('Dingo\Api\Dispatcher')->raw()->post("/api/users/" . \Auth::user()->id . "/objects");
}]);

$api->addRoute(['POST'], 'objects', ['as' => 'api.objects.store', 'uses' => function () {
// works:
return app('Dingo\Api\Dispatcher')->raw()->post("/api/users/" . \Auth::user()->id . "/objects");

// fails due to app('Dingo\Api\Dispatcher')::raw getting reset to false:
//return app('Dingo\Api\Dispatcher')->raw()->post("/api/me/objects");
}]);
```

The issue is that if I nest the `app('Dingo\Api\Dispatcher')->raw()->post()` calls by internally calling /api/abject -> /api/me/objects -> /api/users/{users}/objects then the middle call causes `app('Dingo\Api\Dispatcher')::raw` to get reset to false. Then the outermost call returns an object rather than a Response.

I tracked the issue down to Dispatcher.php's refreshRequestStack()'s `$this->raw = false;` which resets the value. I believe this is due to Dingo\Api\Dispatcher being a singleton. So the dispatcher needs to push its state before queuing each request and pop its state after finishing each request.

I can work around it for now by never nesting internal requests but it's not as elegant as it could be. Thanks for the hard work on Dingo, just thought you'd like to know.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.