“.default”在 Symfony 服务 ID 中有什么特殊意义吗?

Does ".default" have any special significance in Symfony service IDs?

我正在查看 FriendsOfSymfony/FOSOAuthServerBundle 中的一些服务定义,我注意到 fos_oauth_server.storage.default 中有一个定义,但同一文件中的另一个服务列出了 fos_oauth_server.storage,没有 .default 部分,作为依赖项。这里发生了某种通配符的事情,或者......?这是 Symfony 文档不是特别清楚的事情。

id fos_oauth_server.storage 是指向存储服务的别名,默认为 fos_oath_server.storage.default。在您的应用程序配置中,您可以使用自定义服务替换此存储,如捆绑包的 Configuration or the Configuration reference in the docs. Inside the Bundle's Extension-class you can see how the app configuration is wired into the bundle's service configuration and that the alias points to the configured service(默认或您自己的)所示。

所有需要 oauth 存储的服务(包括您的应用程序中的 类)应该只引用别名,以便底层(默认)存储可以轻松替换,而无需触及使用它的代码。

这个概念在捆绑中广泛使用,底层服务的命名约定各不相同。另一种常见的命名方案,特别是如果有多个可以配置的选项,是附加它是什么类型的服务,例如 fos_oauth_server.storage.mysqlfos_oauth_server.storage.redis 等等。除了作为一种更好区分别名和别名可以指向的具体实现的方法之外,.default 在 Symfony 中没有特殊意义。