Skip to content

Commit 9abeb4b

Browse files
committed
When running in console watch for request rebinding and set the route resolver.
When Lumen is running in the console there is no external request which means the service bindings for the request (Application::registerRequestBindings) are never run so the route resolver is never set. This is different from Laravel in that the route resolver is set once the route has been matched from the routing dispatcher. To get around this we'll watch for the rebinding of rebinding of the request object and simply use reflection to grab the currentRoute property and return it for the route resolver. Reference issue dingo#881.
1 parent 978e59c commit 9abeb4b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Provider/LumenServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ public function boot()
1818
{
1919
parent::boot();
2020

21+
// When Lumen is running in the console there is no external request which means the service
22+
// bindings for the request (Application::registerRequestBindings) are never run so the
23+
// route resolver is never set. This is different from Laravel in that the route
24+
// resolver is set once the route has been matched from the routing dispatcher.
25+
// To get around this we'll watch for the rebinding of rebinding of the
26+
// request object and simply use reflection to grab the currentRoute
27+
// property and return it for the route resolver.
28+
if ($this->app->runningInConsole()) {
29+
$this->app->rebinding('Illuminate\Http\Request', function ($app, $request) {
30+
$request->setRouteResolver(function () use ($app) {
31+
$reflection = new ReflectionClass($app);
32+
33+
$property = $reflection->getProperty('currentRoute');
34+
$property->setAccessible(true);
35+
36+
return $property->getValue($app);
37+
});
38+
});
39+
}
40+
2141
$this->app->routeMiddleware([
2242
'api.auth' => 'Dingo\Api\Http\Middleware\Auth',
2343
'api.throttle' => 'Dingo\Api\Http\Middleware\RateLimit',

0 commit comments

Comments
 (0)