diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index cbfcdb224757..ef866235fdec 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -804,7 +804,7 @@ public function substituteImplicitBindings($route) ! $route->getParameter($parameter->name) instanceof Model) { $method = $parameter->isDefaultValueAvailable() ? 'first' : 'firstOrFail'; - $model = $class->newInstance(); + $model = $this->container->make($class->name); $route->setParameter( $parameter->name, $model->where( diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index f0be3e4bd690..c0ffb9bba5f2 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -1155,6 +1155,25 @@ public function testImplicitBindingsWithOptionalParameter() $router->dispatch(Request::create('bar', 'GET'))->getContent(); } + public function testImplicitBindingThroughIOC() + { + $phpunit = $this; + $container = new Container; + $router = new Router(new Dispatcher, $container); + $container->singleton(Registrar::class, function () use ($router) { + return $router; + }); + + $container->bind('RoutingTestUserModel', 'RoutingTestExtendedUserModel'); + $router->get('foo/{bar}', [ + 'middleware' => SubstituteBindings::class, + 'uses' => function (RoutingTestUserModel $bar) use ($phpunit) { + $phpunit->assertInstanceOf(RoutingTestExtendedUserModel::class, $bar); + }, + ]); + $router->dispatch(Request::create('foo/baz', 'GET'))->getContent(); + } + public function testDispatchingCallableActionClasses() { $router = $this->getRouter(); @@ -1390,6 +1409,10 @@ public function firstOrFail() } } +class RoutingTestExtendedUserModel extends RoutingTestUserModel +{ +} + class ActionStub { public function __invoke()