Symfony 5.1 | config/services.php is not working



我正在实现接口自动布线。

config/services.yaml的例子运行得很好,但当使用config/services.php而不是config/services.yaml时,config/service.php中的代码不会被触发。

所以这是有效的:

# config/services.yaml
services:
# ...
AppUtilRot13Transformer: ~
# the ``AppUtilRot13Transformer`` service will be injected when
# an ``AppUtilTransformerInterface`` type-hint is detected
AppUtilTransformerInterface: '@AppUtilRot13Transformer'

但事实并非如此:

// config/services.php
namespace SymfonyComponentDependencyInjectionLoaderConfigurator;
use AppUtilRot13Transformer;
use AppUtilTransformerInterface;
return function(ContainerConfigurator $configurator) {
$services = $configurator->services();
$services->set(Rot13Transformer::class);
// the ``AppUtilRot13Transformer`` service will be injected when
// an ``AppUtilTransformerInterface`` type-hint is detected
$services->alias(TransformerInterface::class, Rot13Transformer::class);
};

这是bug还是我遗漏了什么?

我还在config/services.php中添加了无效的php代码,并且没有抛出任何错误。因此,这让我得出结论,config/services.php根本没有运行。

从5.1开始,Symfony一直在使用基于php的config/services.php和routes.php文件来代替yaml文件进行配置。

但是,您需要删除相应的yaml文件,以便加载php文件。您可以在Kernel::configureContainer((中看到这一点。

至少在我的设置中,我还发现我必须手动运行";bin/console clear:cache";在更改services.php或routes.php之后。缓存不会像处理yaml文件那样自动刷新。可能只是我做错了什么。

基于php的服务和路由构建器非常强大。它们为您提供了大量语法检查和自动完成帮助。非常值得调查。文档中有yaml与xml与php的并列示例。

最新更新