全局访问symfony2的配置值

全局访问symfony2的配置值

本文介绍了全局访问symfony2的配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从应用程序中的任何位置获取配置值(自定义)?

How can I get configuration values (custom) from anywhere in the app?

我想在实体的方法prePersist中的控制器外部进行配置。

I want to do it outside a controller in an entity's method prePersist. Dependency injection sounds illogical here too.

不存在获取配置类或内核的静态方法吗??

Isn't there some static way of getting the config class or the kernel..?

推荐答案

依赖注入是Symfony 2使用配置的方法:为逻辑创建服务,将配置注入服务,以及使用OO逻辑注入其他服务中的服务。至于您的特定问题(在实体的prePersist中使用config),答案是,如果您需要访问配置,则prePersist回调不是执行逻辑的正确位置,因为实体不应该知道属于更高软件层的任何内容(即服务/配置层。)

Dependency Injection is the Symfony 2 way to use configuration: create services for your logic, inject your configuration in services and inject services in other services using OO logic. As for your specific question (using config in the entity's prePersist) the answer is that if you need to access configuration the prePersist callback is not the right place to perform your logic since entities should not be aware of anything that belong to higher software layers (i.e. service/configuration layers).

您可以在此处找到更多说明:

You can find some more explanation here: How to use the translator service inside an Entity?

这篇关于全局访问symfony2的配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:11