使用 Symfony 依赖注入改变 Pimple

Change Pimple with Symfony dependency injection

有没有办法用 Symfony 依赖注入交换 Pimple?

我可以使用 Symfony 依赖注入作为 Silex 的独立包,但我需要用它交换 Pimple,因为我无法访问从 Silex 使用 Symfony 依赖注入注册的控制器,例如:

$app->get('/route', 'testController:indexAction');

不,你不能。 Silex 不允许更改容器,因为 Silex main class 从 Pimple 本身延伸而来。如果您需要 Symfony 容器,您应该使用 Symfony 而不是 Silex。

您可以想出一个解决方案,将对 Pimple 的每次调用都转发到 DIC(Pimple 只是一种代理服务),但我不会走那条路。

请记住,对于即将推出的 Symfony 4(将于 11 月发布,但有 preview already aviable) the framework is going to be more like Silex: instead of having it all and remove the parts you don't use, you'll start small and add components/bundles/libraries into your project (check out Symfony flex)。

最后,作为一个方面 note/fun 事实上,有一个来自 igorw(Silex 的合著者之一)的项目用 Symfony DIC 组件替换了 Pimple,但这更像是一个学术练习而不是现成的使用框架(它确实有效)